Kirk_G Posted July 29, 2003 Posted July 29, 2003 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 Quote
JABE Posted July 29, 2003 Posted July 29, 2003 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. Quote
Kirk_G Posted July 29, 2003 Author Posted July 29, 2003 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 :) Quote
JABE Posted July 29, 2003 Posted July 29, 2003 How about changing this? tempParent = (ParentForm)this.MDIParent; Quote
JABE Posted July 29, 2003 Posted July 29, 2003 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. Quote
Kirk_G Posted July 29, 2003 Author Posted July 29, 2003 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. Quote
anderton Posted July 29, 2003 Posted July 29, 2003 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 Quote
*Experts* mutant Posted July 29, 2003 *Experts* Posted July 29, 2003 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? Quote
Kirk_G Posted July 29, 2003 Author Posted July 29, 2003 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.