MDI child access to it's parents' controls

  • Thread starter Thread starter Lucas Graf
  • Start date Start date
L

Lucas Graf

This sounds so easy to me and feel like an idiot that I can't figure it out,
but....how can I have a MDI child access it's parents control(s)??

Seems like it should be as easy as making the controls on the parent public,
so i created a new project real quick w/2 forms. One is the MDIParent
container and then one child form.

On the MDIParent i put a textbox (textbox1) and set it to public.
From the child code I just try to do a this.MDIParent.textbox1, but there is
no "textbox1" listed under this.MDIParent

What am I doing wrong? Somebody hit me over the head and knock me to my
senses!!
 
Hi Lucas,

You can't see the textbox because MDIParent is stored as a Form object,
which has no textbox.

You need to cast it to your parent class to access its methods.

((MyParent)this.MDIParent).textbox1.Text = "Hello World!";

That said, you should instead use public properties instead of directly
accessing the textbox.

public string TextBoxText
{
set
{
//if(ValidText(value))
textbox1.Text = value;
}
get
{
//if(contentCanBePassed == true)
return textbox1.Text;
//else
// return "";
}
}

This way the parent can keep control of its own textbox.


Happy coding!
Morten Wennevik [C# MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top