Parent And Child Forms

  • 13 years ago

    I have a Parent Form called frmParentMain

    I have a child form called frmGetInfo

    frmParentMain has a MainMenu with FileMenu ---> AddRecord Menu ---> SaveMenu.

    I bring up frmGetInfo with the following code in my AddRecordMenu button

            Dim objGetInfo As New frmGetInfo
            objGetInfo.MdiParent = Me
            objGetInfo.Show()

    I want to use the SaveMenu to execute code to save data entered into textboxes on frmGetInfo, but I cannot access the controls on that form. I could access them in my SaveMenu if I rewrote the statement "Dim objGetInfo As New frmGetInfo" but wont that refer to a another instance of the form and not the one I already have open? Alternatively, I thought of making a MainMenu on the frmGetInfo form, but then I would have to hide the menu, on my Parent Form which is a pain because vb doesn't let you say MainMenu1.Visible = False. I dont want to individually hide all the menu options so that a second menu doesn't appear merged with my Parent Form Menu. Any ideas? Thanks

  • 13 years ago

    You need access to the instance of your form in order to access it.  This is easily accomplished by declaring your variable globally in your class.  Something like this

    Public Class Form1
        Private frmGetInfo As Form2
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If frmGetInfo Is Nothing OrElse frmGetInfo.IsDisposed Then
                ' An instance of the form hasn't been created yet or has been closed
                ' so create a new instance
                frmGetInfo = New Form2
                frmGetInfo.MdiParent = Me
            End If
    
            ' Show the form
            frmGetInfo.Show()
        End Sub
    End Class

    Now because you have declared the variable globally you can access the instance from SaveMenu. However, I don't recommend accessing controls on the GetInfo form. Instead you should create properties on this form that expose the values. This is good programming practice because this way the underlying controls can change, but to your other classes the properties will look the same. It may not seem like a big deal here, but it's best to get yourself used to good habits.

    An alternative approach might be to create a class that represents the information your gathering. Your GetInfo form could accept this class in a constructor. The parent form would pass an instance of this Data class to the GetInfo form which then could update the values when the user enters them. Your SaveMenu would then only need access to your Data class instance instead of knowing about the GetInfo form.

    Also, should your GetInfo form really be a child form. It sounds to me like it's a dialog, but then again I don't know much about the overall application so I don't really know.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”