I know many of you are there sitting at your computers looking at the title going 'what is he on about?'. I would too.
Over the past few weeks, many of the forums I've browsed have nothing on switching between a 'parent' and a 'child' form other than some really complex 'mumbo jumbo'. I am here to show a simple solution.
Also do note, my explanation of parent and child forms is different to that defined in the c# language.
This was the problem:
- You have 2 forms.
- One is named frmParent and the other named frmChild.
- frmParent is your main form which is most of the program.
- frmChild is like a settings type form, which only handles a small part of the program.
Now, you have a button on your main form that says 'settings', when you click on it, you want it to hide your main form, and show the settings form. When you close the settings form, you want it to show the main form again.
Some of the code I have seen to do this is way too complex.
Here is my solution.
For the settings button's Click Event, lets call it btnSettings, we add this code:
Expand|Select|Wrap|Line Numbers
- private void btnSettings_Click(object sender, EventArgs e)
- {
- frmChild child = new frmChild();
- this.Hide();
- child.ShowDialog();
- this.Show();
- }
The this.Hide(); command hides frmParent from view.
The child.ShowDialog(); command pauses frmParent and disables it. Similar to what happens when you summon a MessageBox and you can't use the form behind it. child.ShowDialog(); also shows an instance of frmChild frmParent will remain paused until you close frmChild, and when you do, viola! frmParent reappears because of this.Show();!
Now many forums seem to discuss disposal problems, but in my own opinion, for beginners, this is a good place to start.
Note: I know this article is rough, but it is my first time! :)
mindmadness45