Opening child from child within parent

  • Thread starter Peter W Johnson
  • Start date
P

Peter W Johnson

Hi Guys,

I have a problem opening a child window within a parent window when passing
a variable between two child windows.

I have the parent window setup with two menuitems. The first calls Form2 and
the second calls Form3

The code follows:-

Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem1.Click
Dim NewMDIChild As New Form2
NewMDIChild.MdiParent = Me
NewMDIChild.Show()
End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
Dim NewMDIChild As New Form3
NewMDIChild.MdiParent = Me
NewMDIChild.Show()
End Sub


On Form2 I have a button which calls Form3 with some text I wish to display
in Form3
Form2 Code follows:-

Private ShowForm3 As New Form3(Me)

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Label1.Text = "Text to Send"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim NewMDIChild As New Form3
Dim ShowForm3 = New Form3(Me)
ShowForm3.MdiParent = Me.MdiParent
ShowForm3.Show()
End Sub

The code on Form3 is as follows:-

Private CallingForm As Form2

Public Sub New(ByVal newCallingForm As Form2)
MyBase.New()
InitializeComponent()
CallingForm = newCallingForm
End Sub

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
TextBox1.Text = CallingForm.Label1.Text
End Sub


If I open Form2, click on the button to open Form3 and display the text, all
is OK.
If I only click on "Open Form3" from the mainmenu of Form1 I get an error:-

An unhandled exception of type 'System.OutOfMemoryException' occurred in
system.windows.forms.dll

Additional information: Error creating window handle.

If I edit out the "TextBox1.Text = CallingForm.Label1.Text" statement on
Form2 all is again OK but I can not pass the variable.

Any ideas on how to open Form3 (obviously without the text!)?

Cheers

Peter.
 
C

Cor Ligthert

Peter,

Assuming from your code that Form3 is a child from Form1 do you need to open
it as a child from form1 and not from form2

For that are in the childs the properties MDIParent and in the parent the
MDIChildren

http://msdn.microsoft.com/library/d...stemwindowsformsformclassmdichildrentopic.asp

You need those to do what you want.

You have first to find the index of your form3 in form2

In form2 you work with.

me.mdiparent.mdichildren

I hope this helps,

Cor
 
P

Peter W Johnson

Cor,

You are correct in saying that Form3 is a child of Form1. If I do not
reference form3 to form2 (via the TextBox1.Text = CallingForm.Label1.Text
statement) form3 opens from form1 without any problems.

Form2, a child in form1, opens form3 still with form3 as a child in form1. I
am a bit confused about why I need to find the index of form2 when opening
form3 directly from form1.

Thanks

Peter.
 

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