Search code examples
vb.netvb.net-2010

Text Value of Pop up form (another child form) is not pass to first child form under the MDI Parent form


I am new to VB.Net and created an MDI container in which two child forms "Account_Master" and "F1List" are created. When I open "Account_Master" form and press the F1 key on its TextBoxAccountName, it should open the F1List form which is working ok. After open the F1List, when I enter the text in TextBoxList and Click on Button1, the entered text should be passed to Account_Master's TextBoxAccountName and F1List should close. But this is not happening. here is my code. Please help.

'Form Account_Master code

Private Sub Account_Master_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.F1 Then
        Dim f As New F1List
        f.Show()
    End If
End Sub

'form F1List code

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Account_Master.TextBox1.Text = TextBoxList.Text
    Me.Close()
End Sub

Solution

  • That second code snippet is referring to the default instance of the Account_Master form class. If you didn't display the default instance in the first place then you're making a change to a form other than the one you're looking at. As an example, the first code snippet is NOT displaying the default instance of F1List but, rather, creating an instance explicitly. Displaying the default instance would look like this:

    If e.KeyCode = Keys.F1 Then
        F1List.Show()
    End If
    

    As with your second code snippet, you access the default instance via the class name. If you want to be able to use the default instance later then you need to use the default instance to start with. You can't really mix and match. Most experienced developers would not use them at all, myself included, but use them properly if you're going to use them at all.