ContextMenuStrip.SourceControl property contains a valid control in the case of nested ToolStripMenuItems
Edge
4.7.2
NotPlanned
In the .NET Framework 4.7.1 and previous versions, the xref:System.Windows.Forms.ContextMenuStrip.SourceControl?displayProperty=nameWithType property incorrectly returns null when the user opens the menu from nested xref:System.Windows.Forms.ToolStripMenuItem controls. In the .NET Framework 4.7.2 and later, xref:System.Windows.Forms.ContextMenuStrip.SourceControl property is always set to the actual source control.
- Quirked
- Build-time break
How to opt in or out of these changes
In order for an application to benefit from these changes, it must run on the .NET Framework 4.7.2 or later. The application can benefit from these changes in either of the following ways:
- It targets the .NET Framework 4.7.2. This change is enabled by default on Windows Forms applications that target the .NET Framework 4.7.2 or later.
- It targets the .NET Framework 4.7.1 or an earlier version and opts out of the legacy accessibility behaviors by adding the following AppContext Switch to the
<runtime>
section of the app.config file and setting it tofalse
, as the following example shows.
<runtime>
<AppContextSwitchOverrides value="Switch.System.Windows.Forms.UseLegacyContextMenuStripSourceControlValue=false"/>
</runtime>
Applications that target the .NET Framework 4.7.2 or later, and want to preserve the legacy behavior can opt in to the use of the legacy source control value by explicitly setting this AppContext switch to true
.
P:System.Windows.Forms.ContextMenuStrip.SourceControl
Windows Forms