Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions src/System.Windows.Forms/System/Windows/Forms/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7518,14 +7518,16 @@ protected virtual void OnHandleDestroyed(EventArgs e)
((EventHandler?)Events[s_handleDestroyedEvent])?.Invoke(this, e);

// The Accessibility Object for this Control
if (Properties.TryGetValue(s_accessibilityProperty, out ControlAccessibleObject? accObj))
if (Properties.TryGetValue(s_accessibilityProperty, out AccessibleObject? accObj)
&& accObj is ControlAccessibleObject controlAccObj)
{
accObj.Handle = IntPtr.Zero;
controlAccObj.Handle = IntPtr.Zero;
}

// Private accessibility object for control, used to wrap the object that
// OLEACC.DLL creates to represent the control's non-client (NC) region.
if (Properties.TryGetValue(s_ncAccessibilityProperty, out ControlAccessibleObject? nonClientAccessibleObject))
if (Properties.TryGetValue(s_ncAccessibilityProperty, out AccessibleObject? ncAccObj)
&& ncAccObj is ControlAccessibleObject nonClientAccessibleObject)
{
nonClientAccessibleObject.Handle = IntPtr.Zero;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,19 @@ public void Control_CreateAccessibilityInstance_Invoke_ReturnsExpected(bool crea
Assert.Equal(createHandle, control.IsHandleCreated);
}

[WinFormsFact]
public void Control_OnHandleDestroyed_CustomAccessibleObject_DoesNotThrow1()
{
using CustomCreateAccessibilityInstanceControl control = new()
{
CreateAccessibilityResult = new AccessibleObject()
};

AccessibleObject accessibleObject = control.AccessibilityObject;
Action action = () => control.InvokeOnHandleDestroyed(EventArgs.Empty);
action.Should().NotThrow();
}

[WinFormsFact]
public void Control_CreateControl_Invoke_Success()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ private class CustomCreateAccessibilityInstanceControl : Control
public AccessibleObject CreateAccessibilityResult { get; set; }

protected override AccessibleObject CreateAccessibilityInstance() => CreateAccessibilityResult;

public void InvokeOnHandleDestroyed(EventArgs eventArgs) => OnHandleDestroyed(eventArgs);
}

[WinFormsTheory]
Expand Down