StrangerMan
StrangerMan

Reputation: 21

VB.NET MDI Child set other child property

I hope you can help my trouble. I have 1 form as parent MDI (frmParent.vb) and have 2 child form (frmChild01.vb & frmChild02.vb).

the code for parent form as per below.

Private Sub OpenChild01ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenChild01ToolStripMenuItem.Click
    Dim child01 As frmChild01

    child01 = New frmChild01()
    child01.MdiParent = Me
    child01.Show()
End Sub

Private Sub OpenChild02ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenChild02ToolStripMenuItem.Click
    Dim child02 As frmChild02

    child02 = New frmChild02()
    child02.MdiParent = Me
    child02.Show()
End Sub

frmChild01 have button1
frmChild02 have label1

My problem is how can I set label1.text when user click button1
Thanks in advance...

Upvotes: 2

Views: 6448

Answers (2)

jvlync
jvlync

Reputation: 1

You need to check if ChildForm02 is already loaded. If not you need to load it before you can set its label's text property. It might look something like this:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    If MDIParent1.ChildForm2 Is Nothing OrElse MDIParent1.ChildForm2.Visible = False Then
        MDIParent1.ChildForm2 = New Form2
        MDIParent1.ChildForm2.MdiParent = MDIParent1
        MDIParent1.ChildForm2.Text = "Window "
        MDIParent1.ChildForm2.Show()
    End If
    MDIParent1.ChildForm2.Label1.Text = "your text here"

End Sub

You also need to declare the child forms as Public in MdiParent form so that you can access it anywhere within the solution.

Public ChildForm1 As Form1
Public ChildForm2 As Form2

Upvotes: 0

Rob P.
Rob P.

Reputation: 15091

There are a lot of creative ways you can do this; but ultimately you need to provide a communication channel between Child1 and Child2.

The most direct way would be to Pass a Reference of frmChild02 to frmChild01. You'd need label1 to be public so that frmChidl02 can access it (or you can provide a public method to handle the setting.

That only works if you have a reference to frmChild02 when you create frmChild01. Since you seem to have individual buttons to launch those forms, it might be more complicated. One way to handle this would be to use Events to handle the communication. Have your Mdi Parent listen for/raise events from the child forms. So, when you click the button in frmChild01 have your Mdi parent listen for that event and raise a new event called 'ButtonClickInForm1' or something similar. Have frmChild02 subscribe to that event. If there is an instance of frmChild02 it will respond to the button click and update it's label.

Upvotes: 1

Related Questions