Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
I have a MDI related question. How can you access public variables or public properties of the Parent form from a Child form? Its easy enough to send or retrieve info to the child from the parent, but how does the Child send or retrieve info (variables) from the Parent. I've tried using the this.MDIParent.(blah blah blah) but it doesn't show me any public variables or properties I've created in the Parent. Any help would be appriciated. Thanks
Posted
The MDIParent property is of type Form. To access custom variables and properties, you have to cast this.Parent to your specific form's class first.
Posted

So I have the following code inside the ChildForm:

 

private ParentForm tempParent;

tempParent = (ParentForm)this.Parent;

iFilename = tempParent.Fname;

iNum = tempParent.Num;

 

Where Fname and Num are public properties in the ParentForm. When I run this it crashes and on debugging I find that it is because this.Parent and tempParent are undefined. Any ideas? Thanks, it sounds like I'm on the right track :)

Posted

How did you hook up the MDI child forms to the MDI parent? Can you post that snippet please.

 

Btw, my mistake to have posted converting this.Parent :) ; no wonder your first snippet was (ParentForm)this.Parent.

Posted

Actually I got it working. The problem was where I placed the code. The declaration:

 

private ParentForm tempParent;

 

Was placed as a "global" variable for the ChildForm class and I had written the rest of the code in the constructor of ChildForm. I guess that the MDIParent of the ChildForm had not been set yet. When I moved the code to another Method that was called by a mouse click after the constructor it worked fine. Thanks for your help.

Posted

Hi Kirk

 

I also have some trouble calling mdiparent variables from the mdi child.

 

I have sent a message to you with my emailaddress.

 

Can you please send the code you got working or mabye attach it to a reply to this message.

 

Thanks

 

Jakob

  • *Experts*
Posted
Hi Kirk

 

I also have some trouble calling mdiparent variables from the mdi child.

 

I have sent a message to you with my emailaddress.

 

Can you please send the code you got working or mabye attach it to a reply to this message.

 

Thanks

 

Jakob

 

How does your code look now?

Posted

So here is basically how the code I got working is setup:

 

public class ChildForm : System.Windows.Forms.Form

{

private ParentForm tempParent;

"

"

 

public ChildForm()

{

...

}

 

private void ChildForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)

{

tempParent = (ParentForm)this.MdiParent;

tempParent.sbCoords.Text = @"test";

}

}

 

In my program I have a parent form (type ParentForm) in which I have a statusBar with a panel called sbCoords. This code shows how to send text from a child (ChildForm) to the parent. The important thing to remember is that what ever you want to access in the parent has to be made public (properties, variables, statusBar panels, etc). Hope this helps some other people out there.

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