Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with passing value between forms 1

Status
Not open for further replies.

eseabrook2008

Technical User
Jan 9, 2008
74
CA
I've gone through the article about passing values between forms and ran into a problem.

Here the scenario:
Click a button on FORM1 and open FORM2
FORM2 has a treeview.
Select a node and pass the value back to FORM1 and populate a text box with the value.

It appears to work but once the app ends, the value of the textbox on FORM1 hasn't changed.

FORM1:
Code:
Private Sub Label85_Click
    F2.ShowDialog() <-- open FORM2
End Sub
    
Public Sub SetReceive(ByVal value As String)
    TextBox7.Text = value
    MessageBox.Show("Form 1" & value) <--displays correct
End Sub

FORM2:
Code:
Private Sub Button1_Click

        node = TreeView1.SelectedNode

        Do Until (node.Parent Is Nothing)
            node = node.Parent
        Loop

        MessageBox.Show("Form 2 " & node.ToString) 
        Dim frmReciever As New Form1
        frmReciever.SetReceive(node.ToString)

        me.Close()
End Sub
 
OK, here is a little background. Forms definitions are class definitions. When you are creating your forms in Visual Studio, you are creating the definitions of those forms. When your application runs, you are creating instances of those form definitions.

So when you code Dim frmReciever As New Form1 in Form2, you are not referring to your orginal Form1 instance. You are creating a new instance. So you have two Form1 instances with the same definition, but with the possibility of having different values for their attributes.

So, what you need to do is refer to your original instance of Form1 when you want to set the TextBox value.

Here is some code to help you achieve your goal

Form1
Code:
Private Sub Label85_Click
    F2.ShowDialog() <-- open FORM2
    [b]SetReceive(F2.NodeValue)[/b]
    F2.Close()
End Sub

Code:
[b]Public NodeValue As String = ""[/b] 'Add this to the top of your form, outside any events or sub routines
Private Sub Button1_Click

        node = TreeView1.SelectedNode

        Do Until (node.Parent Is Nothing)
            node = node.Parent
        Loop

        MessageBox.Show("Form 2 " & node.ToString)

        [b]Me.NodeValue = node.ToString[/b]

        me.Close()
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top