Skip to content

Commit

Permalink
Fixing ShowInTaskbar=false doesn't work in some cases (#6421) (#6989)
Browse files Browse the repository at this point in the history
Fixed #6421 - do not exit the form when changing ShowInTaskbar property and re-creating handle
* Add unit test
* Add manual test to Form.ShowInTaskbar
  • Loading branch information
roland5572 authored Apr 18, 2022
1 parent 11bdca7 commit 93c7049
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/System.Windows.Forms/src/System/Windows/Forms/Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6361,7 +6361,10 @@ private void WmNCDestroy(ref Message m)

if (Modal && _dialogResult == DialogResult.None)
{
DialogResult = DialogResult.Cancel;
if (GetState(States.Recreate) == false)
{
DialogResult = DialogResult.Cancel;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public enum MainFormControlsTabOrder
RichTextBoxesButton,
PictureBoxesButton,
FormBorderStylesButton,
FormShowInTaskbarButton,
ToggleIconButton,
ErrorProviderButton,
TaskDialogButton,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Drawing;
using System.Windows.Forms;

namespace WinformsControlsTest
{
public class FormShowInTaskbar : Form
{
public FormShowInTaskbar()
{
Width = 600;
Height = 460;
StartPosition = FormStartPosition.CenterScreen;

var btnTest = new Button()
{
Text = "Click here to open new Form",
Location = new Point(10, 10),
Height = 60,
AutoSize = true,
};

btnTest.Click += BtnTest_Click;
Controls.Add(btnTest);
}

private void BtnTest_Click(object sender, EventArgs e)
{
using var form = new Form()
{
Width = 680,
Height = 400,
StartPosition = FormStartPosition.CenterScreen,
};

var btnTest = new Button()
{
Text = $"Click here to test ShowInTaskbar.{Environment.NewLine}If the test result is failed, this dialog will automatically close, or it will throw an exception.",
Location = new Point(10, 10),
Height = 60,
AutoSize = true,
};

btnTest.Click += (object sender, EventArgs e) =>
{
IntPtr formHandle = form.Handle;
form.ShowInTaskbar = !form.ShowInTaskbar;

if (form.IsHandleCreated == false)
throw new Exception();

if (formHandle == form.Handle)
throw new Exception();
};

form.Controls.Add(btnTest);
form.ShowDialog(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ public MainForm()
MainFormControlsTabOrder.FormBorderStylesButton,
new InitInfo("FormBorderStyles", (obj, e) => new FormBorderStyles().Show(this))
},
{
MainFormControlsTabOrder.FormShowInTaskbarButton,
new InitInfo("FormShowInTaskbar", (obj, e) => new FormShowInTaskbar().Show(this))
},
{
MainFormControlsTabOrder.ToggleIconButton,
new InitInfo("ToggleFormIcon", (obj, e) => ShowIcon = !ShowIcon)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1820,6 +1820,36 @@ public void Form_TransparencyKey_SetNotTopLevelWithHandle_GetReturnsExpected(boo
Assert.Equal(0, createdCallCount);
}

[WinFormsFact]
public void Form_ShowInTaskbar_SetFalse_GetReturnsExpected()
{
// Regression test for https://github.com/dotnet/winforms/issues/6421

using var form = new Form
{
ShowInTaskbar = true,
};

DialogResult expectedDialogResult = DialogResult.OK;

form.Load += (object sender, EventArgs e) =>
{
IntPtr formHandle = form.Handle;
form.ShowInTaskbar = false;

Assert.True(form.IsHandleCreated);
Assert.NotEqual(formHandle, form.Handle);
};

form.Shown += (object sender, EventArgs e) =>
{
form.DialogResult = expectedDialogResult;
};

Assert.Equal(expectedDialogResult, form.ShowDialog());
Assert.Equal(expectedDialogResult, form.DialogResult);
}

public static IEnumerable<object[]> Visible_Set_TestData()
{
foreach (DialogResult dialogResult in Enum.GetValues(typeof(DialogResult)))
Expand Down

0 comments on commit 93c7049

Please sign in to comment.