if you run this snippet of code(put it in form1) in a fresh new winform app with 2 forms
private void Form1_Load(object sender, EventArgs e)
{
Form2 newForm = new Form2();
Button b = new Button();
newForm.Controls.Add(b);
b.Click += new EventHandler(click);
this.Show();
newForm.ShowDialog();
}
private void click(object sender, EventArgs e)
{
开发者_运维问答 ((Form)((Control)sender).Parent).ShowInTaskbar = false;
}
and you click on the button on the new form(should be form2), form2 will close.
How to keep it open?
It is not possible. I actually filed a bug report about it at Microsoft's feedback site but they flipped me the bird on it.
Admittedly, it is a tricky problem to solve, changing the property requires Windows Forms to recreate the window from scratch because it is controlled by a style flag. The kind you can only specify in a CreateWindowEx() call with the dwExStyle argument. Recreating a window makes it difficult to keep it modal, as required by the ShowDialog() method call.
Windows Forms works around a lot of User32 limitations. But not that one.
You keep it open by setting ShowInTaskbar to false before you ShowDialog();
private void Form1_Load(object sender, EventArgs e)
{
Form2 newForm = new Form2();
Button b = new Button();
newForm.Controls.Add(b);
b.Click += new EventHandler(click);
this.Show();
// add this line of code...
newForm.ShowInTaskbar = false;
newForm.ShowDialog();
}
private void click(object sender, EventArgs e)
{
((Form)((Control)sender).Parent).ShowInTaskbar = false;
}
Or just don't make the second form modal. This works also.
private void Form1_Load(object sender, EventArgs e)
{
Form2 newForm = new Form2();
Button b = new Button();
newForm.Controls.Add(b);
b.Click += new EventHandler(click);
this.Show();
newForm.Show();
}
I don't know the specific mechanism here, but clearly what is happening is that changing the flag (which actually changes one or more WS_EX_xxx window styles) is causing the modal pump of ShowDialog() to exit. This, in turn is causing you to (finally!) exit Form1_Load and then your newForm goes out of scope and is destroyed.
So your problem is a compbination of ShowDialog() and the fact that you aren't prepared for ShowDialog() to ever exit.
Now a modal for shouldn't show up with a taskbar icon in the first place, there really should be only 1 taskbar icon for an application and all of it's modal forms, since when a modal form is running, the main form is disabled anyway. When the main form is minimized, all of the modal forms that it owns will be hidden, etc.
So if you really want this second form to be modal, you shouldn't give the user the ability to give it a taskbar icon. If using ShowDialog() was just test code, then don't worry about it. The problem will go away when the form runs on the main application pump.
Your question isn't very clear to me. Anyway, the newForm
form is displayed as a modal dialog, which means that it blocks the user from working with the parent form until it is closed. Modal dialogs usually have some buttons that automatically close them returning either OK
or Cancel
to the calling form (as a return value of ShowDialog
). This is done using the DialogResult
property, so if this property is set for your button, this may be a reason why the modal form closes when you click on it.
If you want to show more forms in a way that allows the user to work with both of them, you'll need to use modeless dialog. Here is a good overview article on MSDN.
how... my... this is an ugly hack
this work
private void Form1_Load(object sender, EventArgs e)
{
Form2 newForm = new Form2();
Button b = new Button();
newForm.Controls.Add(b);
b.Click += new EventHandler(click);
newForm.FormClosed += new FormClosedEventHandler(form2_closed);
newForm.FormClosing += new FormClosingEventHandler(form2_closing);
this.Show();
do
{
newForm.ShowDialog();
} while (newForm.IsDisposed == false );
}
private void click(object sender, EventArgs e)
{
((Form)((Control)sender).Parent).ShowInTaskbar = !((Form)((Control)sender).Parent).ShowInTaskbar;
}
private void form2_closed(object sender, FormClosedEventArgs e)
{
((Form)sender).Dispose();
}
private void form2_closing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.None)
e.Cancel = true;
}
精彩评论