Construction of `AccessibleObject` forces creation of `Handle`
See original GitHub issue- .NET Core Version: .NET 5 P4
Problem description:
Multiple code paths involving a construction or an interaction with an AccessibleObject on a control that hasn’t had its Handle created cause the creation of the Handle.
This may lead to undesired side-effects like described in https://github.com/dotnet/winforms/issues/2797.
Expected behavior:
None of AccessibleObject events must be raised if an owner control doesn’t have a valid `Handle.
https://github.com/dotnet/winforms/blob/b666dc7a94d8ac87a7d300cfb4fa86332fb79bae/src/System.Windows.Forms/src/System/Windows/Forms/AccessibleObject.cs#L2227-L2294
Minimal repro:
Run the following test: https://github.com/dotnet/winforms/blob/b666dc7a94d8ac87a7d300cfb4fa86332fb79bae/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripItemTests.cs#L14005-L14022
Line:14014 will cause ToolStripItem.Handle to be created.
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateHandle() Line 4912 C# Symbols loaded.
System.Windows.Forms.dll!System.Windows.Forms.Control.Handle.get() Line 2464 C# Symbols loaded.
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlAccessibleObject.ControlAccessibleObject(System.Windows.Forms.Control ownerControl = {System.Windows.Forms.ToolStrip}) Line 32 C# Symbols loaded.
System.Windows.Forms.dll!System.Windows.Forms.ToolStrip.ToolStripAccessibleObject.ToolStripAccessibleObject(System.Windows.Forms.ToolStrip owner = {System.Windows.Forms.ToolStrip}) Line 5016 C# Symbols loaded.
System.Windows.Forms.dll!System.Windows.Forms.ToolStrip.CreateAccessibilityInstance() Line 4985 C# Symbols loaded.
System.Windows.Forms.dll!System.Windows.Forms.Control.AccessibilityObject.get() Line 498 C# Symbols loaded.
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.ToolStripItemAccessibleObject.FragmentRoot.get() Line 364 C# Symbols loaded.
System.Windows.Forms.dll!System.Windows.Forms.AccessibleObject.Interop.UiaCore.IRawElementProviderFragment.FragmentRoot.get() Line 819 C# Symbols loaded.
[Native to Managed Transition] Annotated Frame
[Managed to Native Transition] Annotated Frame
System.Windows.Forms.dll!System.Windows.Forms.AccessibleObject.RaiseAutomationEvent(Interop.UiaCore.UIA eventId = AutomationFocusChangedEventId) Line 2267 C# Symbols loaded.
> System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.ToolStripItemAccessibleObject.RaiseFocusChanged() Line 481 C# Symbols loaded.
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.Select() Line 3094 C# Symbols loaded.
System.Windows.Forms.Tests.dll!System.Windows.Forms.Tests.ToolStripItemTests.ToolStripItem_Select_InvokeWithParent_Success() Line 14017 C# Symbols loaded.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (9 by maintainers)

Top Related StackOverflow Question
We could, for example, add the following guard and then run tests:
This guard will crash the test runner, highlighting the paths where we fire off accessibility events on controls without valid handles.
Would this be a correct approach?
We don’t need to change
Handle.getbecause_handleis set when creating controlHandle(OnHandleCreatedmethod). And ifHandleis created then (Owner.Handle == _handle).