Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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...

  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

when I use this

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:

   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

  • *Gurus*
Posted

This is a limitation of value types. You can get around it like this:

 

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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...