| | |
Setting parent and child windows?
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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.
Now, you need to have Form2 store the instance.
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:
Hope this helps,
Jerry
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.
C# Syntax (Toggle Plain Text)
private void button1_Click(object sender, EventArgs e) { button1.Enabled = false; Form2 myForm = new Form2(this); myForm.Show(); }
Now, you need to have Form2 store the instance.
C# Syntax (Toggle Plain Text)
public partial class Form2 : Form { Form1 myParent = null; public Form2(Form1 myParent) { InitializeComponent(); this.myParent = myParent; }
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:
C# Syntax (Toggle Plain Text)
private void button1_Click(object sender, EventArgs e) { myParent.button2.Text = "Foo"; }
Hope this helps,
Jerry
![]() |
Similar Threads
- Variable scope problem (C++)
- Binding a dataset to BindingNavigator (C#)
- Coordinate problems with Windows.SetParent (Pascal and Delphi)
- C# : Client windows in client windows (C#)
- Password (Windows NT / 2000 / XP)
Other Threads in the C# Forum
- Previous Thread: Extracting text between certain tags? c#
- Next Thread: the form won't show..
| Thread Tools | Search this Thread |
.net access algorithm appportability array avltree barchart bitmap box broadcast c# capturing check checkbox client combobox control conversion csharp custom customactions database datagrid datagridview dataset datetime degrees delegate development disappear draganddrop drawing encryption enum error event excel file form format forms function gdi+ http httpwebrequest image index input install java label list listbox mailmerge mandelbrot math monodevelop mouseclick mysql operator path photoshop picturebox pixelinversion pixels post programming radians regex remote remoting resolved. richtextbox rotation server sleep socket sockets sql statistics stream string table text textbox thread time timer transparency update usercontrol validation virtualization visualbasic visualstudio webbrowser windows winforms wpf xml





