Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12661 Null Reference Exception: System.Windows.Forms.TabControl.<WmSelChange> #12683

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,8 +5703,73 @@ 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();
}

[WinFormsFact]
public void TabControl_WmSelChange_AccessibilityObjectNotCreated_NoAutomationEventRaised()
LeafShi1 marked this conversation as resolved.
Show resolved Hide resolved
{
using TabControl tabControl = new();
using SubTabPage tabPage = new();
tabControl.SelectedTab = tabPage;
tabControl.SelectedIndex = 1;

var automationEventRaised = false;
tabPage.TabAccessibilityObject = new TabAccessibilityObject
{
RaiseAutomationEventAction = () => automationEventRaised = true
};

tabControl.TestAccessor().Dynamic.WmSelChange();

Application.DoEvents();
Thread.Sleep(100);

automationEventRaised.Should().BeFalse();
}

public class TabAccessibilityObject
{
public Action RaiseAutomationEventAction { get; set; }
public void RaiseAutomationEvent(UIA_EVENT_ID eventId)
{
RaiseAutomationEventAction?.Invoke();
}
}

public enum UIA_EVENT_ID
{
UIA_SelectionItem_ElementSelectedEventId,
UIA_AutomationFocusChangedEventId
}

private class SubTabPage : TabPage
{
public TabAccessibilityObject TabAccessibilityObject { get; set; }
}

private class NullTextTabPage : TabPage
Expand Down