Hide a modal form and show a new one
- Heres the situation.
I have one main form that I want to show all the time. It has a menu strip at the top. User clicks on an item in menu strip and new modal window is opened which has more options. When the user clicks a button on this modal form, I want another modal form to be opened and the previous one to be hidden. Then when the user closes the current form, the previous form is displayed again. The main form has to be displayed all the time.
Main form -> Menu item click -> open Modal form 1 -> button or any other item clicked on Model form 1 -> open Model form 2 and hide model form 1 ->
user closed model form 2 and model form 1 shows up again.
With what I have right now, if I hide mode form 1, the main form also gets hidden. Basically I am not able to figure out how to only reference model form1 to hide and show.
In the following code example, CreateEditUser and UserList are two different forms.
UserList = model form 1
CreateEditUser = model form 2
private void dgProvidersList_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { CreateEditUser cep = new CreateEditUser(); Form currentForm = UserList.ActiveForm; currentForm.Visible = false; DialogResult result = cep.ShowDialog(); if (result == DialogResult.Cancel) { cep.Dispose(); currentForm.Show(); return; } cep.Dispose(); }
Thanks.
Answers
Bullpit, you can solve the problem another way. Instead of hiding your first modal form, how about moving it far off the screen instead, where it will never be seen. Try something like this:
MySecondForm oForm = new MySecondForm();
this.Location = new Point(this.Location.X, this.Location.Y - 5000);
oForm.ShowDialog();
this.Location = new Point(this.Location.X, this.Location.Y + 5000);
~~Bonnie Berent [C# MVP]- Marked As Answer bybullpit Saturday, December 20, 2008 5:27 PM
- You can move the monitors anywhere; left,right, top or bottom in relation to the main monitor. Mine's up. As I said, the MainForm hiding problem is easily solved using a timer. The problem I see is no way to access the MainForm when all the other forms are shown modally and one of them becomes inaccessable.
Edit: Here's the timer code.
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.ShowDialog();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2();
timer1.Start();
Frm2.ShowDialog();
this.Show();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
this.Hide();
}
}
- Marked As Answer bybullpit Saturday, December 20, 2008 6:43 PM
All Replies
Not 100% sure, but try
currentForm.Hide();
instead of
currentForm.Visible = false;
~~Bonnie Berent [C# MVP]- Are you sure that the MainForm is Hidden and not Minimized?
- Sounds like using Form.ActiveForm is getting you into trouble. This worked as expected:
Mainform:
private void button1_Click(object sender, EventArgs e) {
UserList frm = new UserList();
frm.ShowDialog(this);
}
UserList form:
private void button1_Click(object sender, EventArgs e) {
CreateEditUser frm = new CreateEditUser();
this.Hide();
DialogResult res = frm.ShowDialog(this.Parent);
this.Show();
}
Hans Passant. - I get a variety of results. Sometimes it works as expected, sometimes Form2 is shown and the MainForm is minimized and sometimes both forms are minimized. My guess, is that if you let the events complete using a timer or an invoked delegate, the result will be consistent and as expected. Also, with the IDE closed, the app works as expected.
- Hans code works fine for me. I haven't seen any of the idiosyncracies you describe John. Nothing ever gets minimized.
~~Bonnie Berent [C# MVP] - Thank you all for looking at this.
After considering what John posted, yes, the main form is getting minimized. How do I stop that?
I want my main form which contains the menu strip to be open at all times. The way it is setup right now is that it always stays maxmized. The user cannot minimize or move or restore this form. It stays as it is and I want it that way.
The code posted by Nobugz works the same as mine. The only problem is that the main form is getting minimized.
Note: For all modal forms opened after the main form, only the close option is available to the users and they don't show up in taskbar (ShowInTaskbar = false).
Thanks again,
Max - They appear to be minimized. Actually they are hidden by the IDE. It works fine if the IDE is minimized or in SharpDevelop IDE.
- Ah, ok John ... you must work with your IDE maximized. I never maximize my windows, so I was still seeing my MainForm, even though the IDE showed in front of it. But, now I see why you initially thought that your MainForm was being minimized.
~~Bonnie Berent [C# MVP] - John you are absolutely right. How to solve this problem? I tried closing the IDE and running the application from the exe thinking it would be ok if the IDE is not there but even then the main form gets minimized.
- Why is it a problem? It's only happening in development. When you run the actual app from the EXE it won't do that.
~~Bonnie Berent [C# MVP] - BonnieB said:
Why is it a problem? It's only happening in development. When you run the actual app from the EXE it won't do that.
~~Bonnie Berent [C# MVP]
Ok...now I see the problem. If I have any other window opened and it is maxmized, be it an IE window or a folder window, it does this. If everything else is either closed or minimized, it's not a problem. Is this a bug as I don't think this is the expected behavior? bullpit:
I don't think what you have in mind is a very good idea. If your MainForm is maximized and all your other forms are shown modally and not in the Taskbar, what do you do when one of the modal forms is hidden by the MainForm?
- JohnWein said:
bullpit:
I don't think what you have in mind is a very good idea. If your MainForm is maximized and all your other forms are shown modally and not in the Taskbar, what do you do when one of the modal forms is hidden by the MainForm?
How will it be hidden by the main form? And the problem is it's not my idea. This is what the client wants :(. - Windows is not a perfect OS. What I described can and will happen. Your client won't like it. You'll have to program for the possibility. Why aren't you using a MDI approach?
Bullpit, you can solve the problem another way. Instead of hiding your first modal form, how about moving it far off the screen instead, where it will never be seen. Try something like this:
MySecondForm oForm = new MySecondForm();
this.Location = new Point(this.Location.X, this.Location.Y - 5000);
oForm.ShowDialog();
this.Location = new Point(this.Location.X, this.Location.Y + 5000);
~~Bonnie Berent [C# MVP]- Marked As Answer bybullpit Saturday, December 20, 2008 5:27 PM
- Hmmm...so is there no way around this "main form minimizing" problem? Any other ideas you have that could mimic this scenario but would work all the time?
- Bonnie-
That seems to work like a charm. Thank you very much for enlightning me. I hope there are no side effects of this approach as I had earlier for hiding the form? You're welcome! =0)
I can't see any drawbacks to doing this. It's an old trick. The difference is that when I used to do this "back in the old days", I used to move the form off the screen to the left or right. Now, with many people having more than one monitor, that wouldn't work. Moving it up or down will work though.
~~Bonnie Berent [C# MVP]- Thanks all for the help. I spent like 2 hours trying to figure this out and finally left it with a headache.
- You can move the monitors anywhere; left,right, top or bottom in relation to the main monitor. Mine's up. As I said, the MainForm hiding problem is easily solved using a timer. The problem I see is no way to access the MainForm when all the other forms are shown modally and one of them becomes inaccessable.
Edit: Here's the timer code.
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.ShowDialog();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2();
timer1.Start();
Frm2.ShowDialog();
this.Show();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
this.Hide();
}
}
- Marked As Answer bybullpit Saturday, December 20, 2008 6:43 PM
- John-
This works well as well. Thanks a lot. I agree totally with you about the MainForm becoming inaccssible in a certain situation and I had a scenario earlier when that happened.
I mentioned earlier that all the modal forms also cannot be minimzed or maximized (by design). Would you suggest showing them in taskbar to have access to them at all times? - Nah! Alt-Tab give access and the chances of it happening are slim. We're trying to get it to screw up and, with the timer, I wasn't able to. All your forms are fixed dialog, aren't they? They only have a close button and they can't be minimized?
- What shows or prevents showing the form on the TaskBar is the form's ShowInTaskBar property. Yours is apparently set to false ... set it to true and you're good to go.
Here's the thing with Alt-Tab. What shows depends on whether or not some or all of your modal forms have the ShowInTaskBar property set to false. If your second modal form is set to false, but your first one is set to true, Alt-Tab won't show any of the forms when the second modal form is up (not even the MainForm). Setting them both to true or both to false, and Alt-Tab works as you would expect.
~~Bonnie Berent [C# MVP] - John, Bonnie-
I knew that Alt+Tab would work but was thinking w.r.t regular users and the pain of telling them that they can do that if in case one of the forms becomes inaccessible.
John- Yes, all the forms are fixed dialog (including Main form) and ShowInTaskBar property set to false for all the forms except the main form.

