Link to home
Start Free TrialLog in
Avatar of addicktz
addicktz

asked on

MDI Forms 1/2 - Working Between

Ok, So I am clueless on how to work between forms in vb.net, especially mdi forms, cany anyone give me any suggestions on how I would go about sending a string to the mdiparent form ?
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

That depends on how you want to send the string.  Would you prefer the MDI child to raise an event that the MDI parent can trap, or would you like the MDI Child to set a variable or call a function/sub that resides in the MDI parent?

~IM
Avatar of addicktz
addicktz

ASKER

how bout i raise it to 500 for all three =)
Here is the first method using events.  The mdiChild form has a TextBox and a Button.  When the button is pressed, the text from the textbox is sent the the parent via mdiChilds custom event.  I have excluded all the control declarations so it is easy to see how the code works:

Public Class mdiParent
    Inherits System.Windows.Forms.Form

    Private Sub mdiParent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim c As New mdiChild
        c.MdiParent = Me
        AddHandler c.newValue, AddressOf Me.child_newValue
        c.Show()
    End Sub

    Private Sub child_newValue(ByVal child As mdiChild, ByVal someValue As String)
        MsgBox(someValue, MsgBoxStyle.Information, "newValue() event received in mdiParent")
    End Sub
End Class

Public Class mdiChild
    Inherits System.Windows.Forms.Form

    Public Event newValue(ByVal child As mdiChild, ByVal someValue As String)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent newValue(Me, TextBox1.Text)
    End Sub
End Class
Here is the second method where the child changes a variable in the parent:

Public Class mdiParent
    Inherits System.Windows.Forms.Form

    Public someValue As String

    Private Sub mdiParent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim c As New mdiChild
        c.MdiParent = Me
        c.Show()
    End Sub
End Class

Public Class mdiChild
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mdiP As mdiParent = CType(Me.MdiParent, mdiParent)
        mdiP.someValue = TextBox1.Text
        MsgBox(mdiP.someValue, MsgBoxStyle.Information, "someValue on mdiParent changed")
    End Sub
End Class
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you =)