MdiChild and Public declarations

Salat

Regular
Joined
Mar 5, 2002
Messages
81
Location
Poland, Silesia
When I declare some public data in MdiChild, how can I get them from MdiParentForm.

I thought, I can use in MdiForm code:

Me.ActiveMdiChild.SomePublicData

there is so much to learn in vb .net, I hope You won't hate me beacouse I'm aksing so often and I'm not always easy to understand :) I mean my english isn't well. Anyway once again: Please help me... someday I will help the noviceres as You do...
 
To be able to see the public field SomePublicData, you'll have to cast the ActiveMdiChild property to the exact class type of the form you're referencing. In .NET, all forms inherit from a generic "Form" class, which is what ActiveMdiChild exposes. Your form adds a bunch of functionality to the base Form class, such as your SomePublicData variable. So even though it's public, someone with a reference to a generic Form object can't see it.

If your child form's class name is frmChild then use this code:
DirectCast(Me.ActiveMdiChild, frmChild).SomePublicData
(at least I *think* that's right in VB.NET... more of a C# guy :))

If you're not sure what the form is (because you have multiple child forms, each based on a different class), you can check the ActiveMdiChild form to see what type it is and cast to the appropriate type. OR, if all of you child forms need this variable called "SomePublicData" then you could create an Interface and have each child form implement that interface. Then the MDI Parent form could use DirectCast on the ActiveMdiChild form to cast it as the interface, which exposes whatever it is you need.

Let us know if you need help with the second option :)

-Nerseus
 
when I use this
Code:
DirectCast(Me.ActiveMdiChild, Form2).DC.Edytowalny
the following error occurs:
Cannot refer to 'Edytowalny' because it is a member of the value-typed field 'DC' of class 'EasyForms.Form2' which has 'System.MarshalByRefObject' as a base class.


and the declarations in a Form2 looks like that:
Visual Basic:
    Public Structure typedc
        Public Tytuł As String
        Public Autor As String
        Public Edytowalny As Boolean
    End Structure
    Public DC As typedc
I don't get it
 
This is a limitation of value types. You can get around it like this:

Visual Basic:
Dim d As typedc = DirectCast(Me.ActiveMdiChild, Form2).DC
'Now you can access d.Edytowalny

Note that as a value type, any changes you make to d won't affect the DC property on Form2. You'd have to change d and then assign it back to make another copy.
 
Thank You guys... it was a bit complicatd, but I've handled it somehow.

Nerseus: at this time, I think I gona use only one MdiChild, but who knows, maybe while writing a program I will have to use more then one mdichild. Then I probably ask You about some help

once again... thank You
 
Back
Top