MdiChild and Public declarations

Salat

Well-known member
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 wont hate me beacouse Im aksing so often and Im not always easy to understand :) I mean my english isnt 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, youll have to cast the ActiveMdiChild property to the exact class type of the form youre 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 its public, someone with a reference to a generic Form object cant see it.

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

If youre 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:
Code:
    Public Structure typedc
        Public Tytuł As String
        Public Autor As String
        Public Edytowalny As Boolean
    End Structure
    Public DC As typedc
I dont get it
 
This is a limitation of value types. You can get around it like this:

Code:
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 wont affect the DC property on Form2. Youd have to change d and then assign it back to make another copy.
 
Thank You guys... it was a bit complicatd, but Ive 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