2 form program

lothos12345

Junior Contributor
Joined
May 2, 2002
Messages
294
Location
Texas
I have a program with 2 forms and one textbox in each form. I want the value of the textbox in form one to appear in the textbox on form two. And if the users changes the value in of the string in form two and click the "Ok" button on form 2 I want form 2 to close and the altered string to appear back the the textbox on form 1. Basically I need help is understanding how to pass values back from one form to another. Any examples would be greatly appreciated.
 
Assuming form1 is the parent or main form, it would instatiate form2 and then have direct access to all controls on form2.

If in form1 you neeed data from a textbox in form2: form2.someTextBox.Text()

A trick you could use to get the data from form2 back into form1 when a button on form2 is clicked is to hide form2 when the button is clicked. The flow in form1 would go something like (shooting from hip):

Visual Basic:
Public Sub CallForm2()
   dim childForm as Form2 = new Form2
   childForm.show(Me)  'makes childForm modal and thus a blocking call.
                                ' this form (me) will wait until childForm returns
   ''childform is hidden or closed
   if not childForm is nothing then   ''the 'X' could have been hit.  Search this forum for ways to override it if you want.
      Me.someTextBox.Text = childForm.someTextBox.Text
   End If
   childForm.close()  ''kill the child form for all time.
End Sub

Meanwhile in the child form, you are going to call Me.Hide() when the button is clicked. You may want to override the X button to prevent the form from being complete closed as well.

Also, I'm not completely satisified with this solution, but I have used it several times... Does anyone know of a better way to handle this?
 
Another Problem

Would work however, the procedure is already in a sub and my boss wants me to close form2 when I click the "OK" button on form2 not Hide it. He just wants the variable passed back to form1 (the parent) before form2 closes all this accomplished with the click of the "OK" button on form2.
 
That is the standard way to do it. It would seem your boss doesn't quite understand UI proramming. Your other options are to pass a ref to Form1 or pass a delegate when you create Form2, but that is overkill.
 
Ahh...another case of a Project Manager putting undue pressure on his coders and attempting to solve their problems when he reall has no idea what he is talking about...

The simple answer is to just not show your boss the code. The hide/rip/close will all take place so fast that he'll never no the difference. The end result of the hid/rip/close will look exactly the same as the delgate way.

I kind of dig the delagate idea though. Why would that or a reference to antoher variable be considered overkill? I almost like that way better...

Regardles, it is horribly annoying when the management wants to write the program... I'd say to them...good luck.
 
If you want to use form2 as a dialog box, this is how i do it:

Visual Basic:
'This code goes in Form2
'
Dim _Result As String
Public Readonly Property Result
    Get
        Return _Result
    End Get
End Property
'
Shared Function ShowDialog() As String
    Dim frlDialog As New Form2
    Form2.ShowDialog() 'Instantiate and show form2
    Dim Result As String = Form2.Result 'Get the result
    Form2.Close 'Close the form
    Return Result 'And return the result
End Function
'
Sub btnOK_Click(Sender As Object, e As EventArgs) Handles btnOK.Click
    _Result = Textbox1.text 'Result property will return text entered
    Me.Hide 'Hide this form to return control to Shared ShowDialog()
End Sub

If you are going to do this alot you don't want to repeatedly create Form2s though, this will be a lot of work for the garbage collector. In that case I would instantiate Form2 once and re-use it repeatedly.
 
mskeel said:
I kind of dig the delagate idea though. Why would that or a reference to antoher variable be considered overkill? I almost like that way better...

It's better OOP practice if object2 doesn't need to know about object1.
 
Good Ideal but

Marble_eater has a good ideal, but how do I pass the value in textbox1 on form 1 to the textbox1 on form2 for editing and then pass it back to form 1 once the "OK" button on form2 is clicked?
 
I was actually gonna point that thread out, but I didn't. Anyways...

If you use the code I posted above for Form2, in form1's code you would just do this:

Visual Basic:
Me.TextBox1 = Form2.ShowDialog()

That's it. Whenever you want to use Form2 to get a string, call Form2.ShowDialog() and use the return value.
 
So instead of handling all the cleanup from the parent's side you handle it from the Child's side by overriding the child's showdialog method, yes?

Result is an property that you can override in forms to return results as anything you want?

That is what you were saying, yes?

If I understand this correctly, this is pretty sweet, and I think the answer I've been looking for. The one thing I never really liked about the way that I did it above is that it still required the child to have knowledge of the parent - it had to know that it needed to return a value and be hidden. I always felt that coupled the two together too much.
 
Another Problem

Everytime i try to use the shared function it in form2 it gives me the following error:

C:\Documents and Settings\jgonzales\Desktop\Working Copy of PTNotes\Copy of PTProgNote working\frmEmail.vb(91): 'Public Shared Function showdialog() As String' and 'Public Function ShowDialog() As System.Windows.Forms.DialogResult' cannot overload each other because they differ only by return types. 'Shadows' assumed.


Shared Function ShowDialog() as string
'some code'
End Function

The showdialog() is underlined in blue and is giving me the error. On that word. Please help. And as always any help given is greatly appreciated.
 
Code example

Below is an example of some code:

Public Property Getrptid() As String
Get
Return Convert.ToString(rptid)
End Get
Set(ByVal Value As String)
rptid = Convert.ToInt32(Value)
End Set
End Property

Using an example like this code in form2. I need to pass the variable from form1 into a Property on form2 like the above listed and return the variable to form1. On the click of the OK button on form2 the variable gets passed back and form2 closes. Can anyone give me an example using the code listed above, to accomplish my code task? Any help given is greatly appreciated.
 
How????

How do I accomplish this or can you give me the same example has you did earlier using a different name other than "ShowDialog()" or an example of how to create a constructor.
 
Back
Top