Home All Groups Group Topic Archive Search About

MDIChild form controls: how to pass information between them?

Author
11 Apr 2005 12:17 PM
LCAdeveloper
Can anyone help me with this? I'm trying to get an MDIChild form to lookup
the value of a control on another open MDIChild form, but can't seem to get
the right syntax.

Let's say the two child forms are Form1 and Form2. In VB4 I could use (in
Form2):
\\
If Form1.Option1.Value = True Then
//

However, in vb.NET 2003, even if I put the following in the Form2's Load event
\\
Dim MDIa as Form1
.....
If MDIa.Radiobutton1.Checked=True Then
//
I get the usual 'An unhandled exception of type
'System.NullReferenceException' message .... 'Object reference not set to an
instance of of an object'

even though when I put the cursor over the code 'MDIa.' a pop-up message
says 'Dim MDIa as ParentFileName.Form1' (the same pop-up says 'MDIa =
Nothing' if you Break aftyer the above runtime error occurs).

I have tried using 'Dim MDIa as New Form1' instead, which doesn't produce
the error but becasue I have declared it as 'New Form1' the default
Radionbutton on the form is checked instead of the one that I know is checked
on the (hidden) form (at least that is what I think is happening).

How can I get MDIChild Form2 to tell me the current status of a control on
MDIChild Form1. I have even tried going via the Parent form using some code
that (O)enone kindly gave me a couple of weeks ago, but I don't yet have
sufficient ability in vb.NET 2003 to correctly setup the syntax.

Many thanks in advance for any help that anyone can give me.

Author
11 Apr 2005 12:38 PM
Oenone
LCAdeveloper wrote:
[...]
> How can I get MDIChild Form2 to tell me the current status of a
> control on MDIChild Form1.

You need to get a reference to the actual instance of Form1. Declaring a
Form1 object simply gets you a pointer or a brand new form, not the existing
instance.

To get the instance, first use the MdiParent property of Form2. This will
return you the MDI parent form that contains Form1 and Form2.

From here, you can access the MDI form's MDIChildren array, which contains
all of the child forms that are currently open within the MDI parent. One of
them will be your Form1 form, and from there you can read the control
properties you are after.

I personally use the following function which I have added to my MDI form:


\\\
    '
    ' Scans all the MDI Child forms looking for one of the specified type.
    ' If one is found, it will be returned, otherwise Nothing is returned.
    '
    Public Function FindChildForm(ByVal SearchFormType As Type, Optional
ByVal FormCaption As String = "*") As Form

        Dim f As Form

        For Each f In Me.MdiChildren
            If TypeName(f) = SearchFormType.Name And f.Text Like FormCaption
Then
                f.Activate()
                Return f
            End If
        Next

        Return Nothing

    End Function
///

This allows you to pass a Form type and optionally a caption (which allows
you to distinguish between multiple instances of the same form).

To use it, from Form2, try this code:

\\\
    Dim myMDI as MyMDIForm    'change to whatever class your MDI uses
    Dim myForm1 as Form1

    'Get a reference to the MDI parent form
    myMDI = DirectCast(Me.MdiParent, MyMDIForm)

    'Get a reference to the Form1 form
    myForm1 = DirectCast(myMDI.FindChildForm(GetType(Form1)), Form1)

    If myForm1 Is Nothing Then
        MsgBox("Couldn't find Form1")
    Else
        MsgBox(myForm1.RadioButton1.Checked)
    Endif
///

(That code typed straight into this message and so not tested, but it should
be close enough :-)

--

(O)enone
Author
11 Apr 2005 1:25 PM
LCAdeveloper
Thanks very much for your reply, I'll give what you suggest a whirl. :-)

Show quoteHide quote
"Oenone" wrote:

> LCAdeveloper wrote:
> [...]
> > How can I get MDIChild Form2 to tell me the current status of a
> > control on MDIChild Form1.
>
> You need to get a reference to the actual instance of Form1. Declaring a
> Form1 object simply gets you a pointer or a brand new form, not the existing
> instance.
>
> To get the instance, first use the MdiParent property of Form2. This will
> return you the MDI parent form that contains Form1 and Form2.
>
> From here, you can access the MDI form's MDIChildren array, which contains
> all of the child forms that are currently open within the MDI parent. One of
> them will be your Form1 form, and from there you can read the control
> properties you are after.
>
> I personally use the following function which I have added to my MDI form:
>
>
> \\\
>     '
>     ' Scans all the MDI Child forms looking for one of the specified type.
>     ' If one is found, it will be returned, otherwise Nothing is returned.
>     '
>     Public Function FindChildForm(ByVal SearchFormType As Type, Optional
> ByVal FormCaption As String = "*") As Form
>
>         Dim f As Form
>
>         For Each f In Me.MdiChildren
>             If TypeName(f) = SearchFormType.Name And f.Text Like FormCaption
> Then
>                 f.Activate()
>                 Return f
>             End If
>         Next
>
>         Return Nothing
>
>     End Function
> ///
>
> This allows you to pass a Form type and optionally a caption (which allows
> you to distinguish between multiple instances of the same form).
>
> To use it, from Form2, try this code:
>
> \\\
>     Dim myMDI as MyMDIForm    'change to whatever class your MDI uses
>     Dim myForm1 as Form1
>
>     'Get a reference to the MDI parent form
>     myMDI = DirectCast(Me.MdiParent, MyMDIForm)
>
>     'Get a reference to the Form1 form
>     myForm1 = DirectCast(myMDI.FindChildForm(GetType(Form1)), Form1)
>
>     If myForm1 Is Nothing Then
>         MsgBox("Couldn't find Form1")
>     Else
>         MsgBox(myForm1.RadioButton1.Checked)
>     Endif
> ///
>
> (That code typed straight into this message and so not tested, but it should
> be close enough :-)
>
> --
>
> (O)enone
>
>
>
>