Setting parent and child windows?

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 8
Reputation: lorentz is an unknown quantity at this point 
Solved Threads: 0
lorentz lorentz is offline Offline
Newbie Poster

Setting parent and child windows?

 
0
  #1
Apr 29th, 2008
Hey!

I have a problem with parent and child windows. How do I set the main program to parent window and the other windows to child? I also wanna be able to change a line of text in the main (parent) window from a textbox in a child window. How would I do that?


Thanks in advnace!
//Andreas
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Setting parent and child windows?

 
0
  #2
Apr 29th, 2008
There is a variety of ways to do this. I will show you the one I use most often.

It all boils down to variable scoping. Form2 (child) needs to be able to see Form1(parent).
There are two properties in a Form (Parent and ParentForm), however you can not rely upon them being set because of the number of ways to create a form.
Therefore, what I typically do is pass the main form instance to the child form constructor.

  1.  
  2. private void button1_Click(object sender, EventArgs e)
  3. {
  4. button1.Enabled = false;
  5. Form2 myForm = new Form2(this);
  6. myForm.Show();
  7. }

Now, you need to have Form2 store the instance.
  1. public partial class Form2 : Form
  2. {
  3. Form1 myParent = null;
  4.  
  5. public Form2(Form1 myParent)
  6. {
  7. InitializeComponent();
  8. this.myParent = myParent;
  9. }

Once the child has this instance you can call any public method or property in the parent form. So in this example, I selected to set the modifier property of a button on the main form to public. Now Form2 can set the text of form1's button like this:

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.  
  4. myParent.button2.Text = "Foo";
  5. }

Hope this helps,

Jerry
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: lorentz is an unknown quantity at this point 
Solved Threads: 0
lorentz lorentz is offline Offline
Newbie Poster

Re: Setting parent and child windows?

 
0
  #3
Apr 30th, 2008
Thank you! I'm gonna give this a try.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC