passing values between sibling forms

J

Jeff

I'm using vb in a windows application in visual studio 2005.

I'm new to this and to using parent and child forms.

'In the parent form (as an MDIParent) I have:

Private Sub BtnOpenChild1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnOpenChild1.Click
Dim Child1 As New Child1
Child1.MdiParent = Me
Child1.Show()
End Sub

Private Sub BtnOpenChild2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnOpenChild2.Click
Dim Child2 As New Child2
Child2.MdiParent = Me
Child2.Show()
End Sub

'In Child1 form I have:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Parent1.Label1.Text = ListBox1.SelectedItem
Child2.Label1.Text = ListBox1.SelectedItem '<- this doesn't
work
End Sub

' the above works fine to transfer the value from Child1 listbox1's selected
item to lable1 of the parent form, but I can't figure out how to transfer
the
' same value to a similar label in Child2.

' I've seen several discussions about this on the web, but they seem to be
written for older versions of vb.net, and I'm either not understanding
something or they
don't apply to my version.

Can someone help? (you'll likely have to be relatively specific, since this
is new to me)

Thanks
Jeff
 
J

J.B. Moreno

Jeff <[email protected]> said:
I'm using vb in a windows application in visual studio 2005.

I'm new to this and to using parent and child forms.

'In the parent form (as an MDIParent) I have:

Private Sub BtnOpenChild1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnOpenChild1.Click
Dim Child1 As New Child1
Child1.MdiParent = Me
Child1.Show()
End Sub

Private Sub BtnOpenChild2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnOpenChild2.Click
Dim Child2 As New Child2
Child2.MdiParent = Me
Child2.Show()
End Sub

'In Child1 form I have:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Parent1.Label1.Text = ListBox1.SelectedItem
Child2.Label1.Text = ListBox1.SelectedItem '<- this doesn't work
End Sub

' the above works fine to transfer the value from Child1 listbox1's selected
item to lable1 of the parent form, but I can't figure out how to transfer
the
' same value to a similar label in Child2.

The problem is that Child1 doesn't have a reference to Child2, and
neither does Parent.

Quickest solution (assuming that you only have 1 Child1 and 1 Child2
open at the same time) is for the parent to have 2 public properties, 1
for Child1 and 1 for Child2 that can be referenced in your listbox1
event.
 
A

Armin Zingler

Am 09.08.2010 17:47, schrieb Jeff:
I'm using vb in a windows application in visual studio 2005.

I'm new to this and to using parent and child forms.

'In the parent form (as an MDIParent) I have:

Private Sub BtnOpenChild1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnOpenChild1.Click
Dim Child1 As New Child1
Child1.MdiParent = Me
Child1.Show()
End Sub

Private Sub BtnOpenChild2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnOpenChild2.Click
Dim Child2 As New Child2
Child2.MdiParent = Me
Child2.Show()
End Sub

'In Child1 form I have:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Parent1.Label1.Text = ListBox1.SelectedItem
Child2.Label1.Text = ListBox1.SelectedItem '<- this doesn't
work
End Sub

' the above works fine to transfer the value from Child1 listbox1's selected
item to lable1 of the parent form, but I can't figure out how to transfer
the
' same value to a similar label in Child2.

' I've seen several discussions about this on the web, but they seem to be
written for older versions of vb.net, and I'm either not understanding
something or they
don't apply to my version.

Can someone help? (you'll likely have to be relatively specific, since this
is new to me)

In Child1, add an event that is raised in the SelectedIndexChanged event
handler. Handle the new event in the MDIParent. There you can set a (new)
property in Child2 that internally sets the Label's text.

Inside the MdiParent, declare Child1 and Child2 as fields, not as local
variables because they are lost at the end of the sub, and you need a
reference to access an object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top