Skip to content

Commit

Permalink
Fix #12661 Null Reference Exception: System.Windows.Forms.TabControl.…
Browse files Browse the repository at this point in the history
…<WmSelChange> (#12683)

* Fix issue 12661 and add unit test

* Updated the unit test code lines to resolve the conversations from Tanya

* Updated the BeginInvoke delegate in WmSelChange()

* Removed that TabControl_WmSelChange_AccessibilityObjectNotCreated_NoAutomationEventRaised() test

* Removed the unnecessary code lines from TabControlTests.cs file.
  • Loading branch information
John-Qiao authored Jan 8, 2025
1 parent a3da2c9 commit 87bfb04
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,14 @@ private bool WmSelChange()
if (IsAccessibilityObjectCreated && SelectedTab?.ParentInternal is TabControl)
{
SelectedTab.TabAccessibilityObject.RaiseAutomationEvent(UIA_EVENT_ID.UIA_SelectionItem_ElementSelectedEventId);
BeginInvoke((MethodInvoker)(() => SelectedTab.TabAccessibilityObject.RaiseAutomationEvent(UIA_EVENT_ID.UIA_AutomationFocusChangedEventId)));
BeginInvoke((MethodInvoker)(() =>
{
if (IsAccessibilityObjectCreated && SelectedTab?.ParentInternal is TabControl &&
!SelectedTab.IsDisposed && SelectedTab.TabAccessibilityObject is not null)
{
SelectedTab.TabAccessibilityObject.RaiseAutomationEvent(UIA_EVENT_ID.UIA_AutomationFocusChangedEventId);
}
}));
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5703,6 +5703,33 @@ public void TabControl_Invokes_SetToolTip_IfExternalToolTipIsSet()
Assert.Equal(text, actual);
}

[WinFormsFact]
public void TabControl_WmSelChange_SelectedTabIsNull_DoesNotThrowException()
{
using Form form = new();
using TabControl control = new();
using TabPage page1 = new("text1");
control.TabPages.Add(page1);
_ = control.AccessibilityObject;

form.Controls.Add(control);
form.Show();
control.SelectedIndex = 0;

Action act = () => control.TestAccessor().Dynamic.WmSelChange();
act.Should().NotThrow();

control.TabPages.Clear();

var exception = Record.Exception(() =>
{
Application.DoEvents();
Thread.Sleep(100);
});

exception.Should().BeNull();
}

private class SubTabPage : TabPage
{
}
Expand Down

0 comments on commit 87bfb04

Please sign in to comment.