Fix: ContextMenu submenus fire Popup on non-Form controls#17
Merged
Conversation
WM_INITMENUPOPUP is now handled in Control to dispatch to ContextMenu.ProcessInitMenuPopup, ensuring MenuItem.Popup events fire for submenus shown from non-Form controls (e.g., DataGrid). This enables dynamic submenu population and fixes the issue where placeholder items were never replaced. The MenuStackForm demo is updated to use a DataGrid with dynamic submenus to visually repro the bug. Regression tests are added to verify correct Popup event firing for both direct and nested submenus.
srijan-dev8
approved these changes
May 13, 2026
wtg-mg8
approved these changes
May 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ContextMenuSubmenuPopupEvents Not Firing — Root Cause & FixProblem
On .NET 10, the
MenuItem.Popupevent does not fire for submenus when aContextMenuis shownvia
TrackPopupMenuEx. This is a regression from .NET Framework / WinForms 3.0, where the eventfired correctly.
Any pattern that relies on
MenuItem.Popupto populate a submenu dynamically (lazy-loading,placeholder replacement, etc.) is silently broken.
Root Cause
How
WM_INITMENUPOPUPreaches aContextMenuContextMenu.Show()callsTrackPopupMenuEx, passing the owner control's HWND:OnPopupfires immediately for the top-level menu. For each submenu that is about to open,Windows sends
WM_INITMENUPOPUPto the owner HWND. The correct handler callsContextMenu.ProcessInitMenuPopup, which recursively finds the matchingMenuItemby its nativeHMENU handle and fires
MenuItem.OnPopup→Popupevent.The bug:
Control.WndProcsilently dropsWM_INITMENUPOPUPFile:
src/System.Windows.Forms/System/Windows/Forms/Control.csWM_INITMENUPOPUPis sent to the owner control's HWND, not the form's HWND. Because allcontrols inherit
WndProcfromControl, andControl.WndProclumps this message into thedefaultfall-through,ContextMenu.ProcessInitMenuPopupis never reached and no submenuPopupevent fires.Why
Form.WmInitMenuPopupdoes not helpForm.cshas its ownWmInitMenuPopup, but it only handles theMainMenu(menu bar):Additionally,
WM_INITMENUPOPUPis sent to the owner control's HWND (which is typically notthe form), so
Form.WmInitMenuPopupis not even reached forContextMenusubmenus.Reference: WinForms 3.0 Has the Correct Pattern
The upstream
dotnet/winformsrepository atrelease/3.0handled this correctly inControl.cs.The WTG WinForms fork migrated the legacy
ContextMenu/MenuItem/Menutypes but did not portthis message handling.
Source (online):
WmInitMenuPopupmethod (L12333–L12345)WndProcswitch case (L13430–L13431)WinForms 3.0 approach:
Fix Applied
File:
src/System.Windows.Forms/System/Windows/Forms/Control.cs1. New
WmInitMenuPopupmethod (placed nearWmContextMenu)2.
WM_INITMENUPOPUPbroken out of the fall-through group inWndProcRegression Tests
File:
src/System.Windows.Forms.Legacy/System.Windows.Forms.Legacy.Tests/ContextMenu/ContextMenuSubMenuPopupTests.csControl_WmInitMenuPopup_DirectSubMenu_FiresPopupEventControlwith aContextMenuwhoseMenuItemhas a placeholder child. SendsWM_INITMENUPOPUPfor that submenu's HMENU. AssertsPopupfired and the placeholder was replaced.Control_WmInitMenuPopup_NestedSubMenu_FiresPopupEventProcessInitMenuPopuprecurses correctly viaFindMenuItem.Both tests fail before the fix and pass after.
Files Changed
src/System.Windows.Forms/System/Windows/Forms/Control.csWmInitMenuPopup; routedWM_INITMENUPOPUPcase to it inWndProcsrc/System.Windows.Forms.Legacy/System.Windows.Forms.Legacy.Tests/ContextMenu/ContextMenuSubMenuPopupTests.cs[StaFact]tests)