From 2a53da23e0f26e648a742968e8cd7c022ea93689 Mon Sep 17 00:00:00 2001 From: JayashreeSF3546 Date: Wed, 8 Jul 2026 17:14:55 +0530 Subject: [PATCH] Fix WebBrowser control blocks accelerators of buttons on form --- .../Controls/WebBrowser/WebBrowserBase.cs | 2 +- .../WebBrowserBaseTests.cs | 191 ++++++++++++++++++ 2 files changed, 192 insertions(+), 1 deletion(-) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/WebBrowser/WebBrowserBase.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/WebBrowser/WebBrowserBase.cs index d73bf00a64a..cbce29cc729 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/WebBrowser/WebBrowserBase.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/WebBrowser/WebBrowserBase.cs @@ -339,7 +339,7 @@ protected internal override unsafe bool ProcessMnemonic(char charCode) PInvoke.GetCursorPos(out Point p); msg.pt = p; - if (!PInvoke.IsAccelerator(new HandleRef(this, controlInfo.hAccel), controlInfo.cAccel, &msg, lpwCmd: null)) + if (PInvoke.IsAccelerator(new HandleRef(this, controlInfo.hAccel), controlInfo.cAccel, &msg, lpwCmd: null)) { _axOleControl.OnMnemonic(&msg); Focus(); diff --git a/src/test/unit/System.Windows.Forms/WebBrowserBaseTests.cs b/src/test/unit/System.Windows.Forms/WebBrowserBaseTests.cs index f53642aae26..d7c60e32146 100644 --- a/src/test/unit/System.Windows.Forms/WebBrowserBaseTests.cs +++ b/src/test/unit/System.Windows.Forms/WebBrowserBaseTests.cs @@ -858,6 +858,182 @@ public void WebBrowserBase_UseWaitCursor_ThrowsNotSupportedException() Assert.Throws(() => control.UseWaitCursor = true); } + [WinFormsFact] + public void UserControl_ProcessMnemonic_WebBrowserBeforeButton_InvokesButtonMnemonic() + { + using TestForm form = new(); + using TestUserControl userControl = new(); + + TextBox textBox = new() + { + Name = "textBox1", + TabIndex = 0, + Text = "textbox on user control" + }; + + Panel panel = new() + { + Name = "panel1", + TabIndex = 7 + }; + + WebBrowser webBrowser = new() + { + Name = "webBrowserControl", + TabIndex = 0, + Url = new Uri("about:blank"), + WebBrowserShortcutsEnabled = false + }; + + Button button = new() + { + Name = "button1", + TabIndex = 8, + Text = "&Button on user control" + }; + + int clickCount = 0; + button.Click += (_, _) => clickCount++; + + panel.Controls.Add(webBrowser); + userControl.Controls.Add(textBox); + userControl.Controls.Add(panel); + userControl.Controls.Add(button); + form.Controls.Add(userControl); + + form.Show(); + textBox.Focus(); + + Assert.True(textBox.Focused || textBox.ContainsFocus); + + bool processed = userControl.InvokeProcessMnemonic('B'); + + Assert.True(processed); + Assert.Equal(1, clickCount); + } + + [WinFormsFact] + public void Form_ProcessMnemonic_WebBrowserInChildControl_DoesNotBlockFormMnemonic() + { + using TestForm form = new(); + using TestUserControl userControl = new(); + + TextBox textBox = new() + { + TabIndex = 0 + }; + + Panel panel = new() + { + TabIndex = 7 + }; + + WebBrowser webBrowser = new() + { + TabIndex = 0, + Url = new Uri("about:blank"), + WebBrowserShortcutsEnabled = false + }; + + Button childButton = new() + { + TabIndex = 8, + Text = "&Button on user control" + }; + + Button okButton = new() + { + Text = "&OK" + }; + + int okClickCount = 0; + okButton.Click += (_, _) => okClickCount++; + + panel.Controls.Add(webBrowser); + userControl.Controls.Add(textBox); + userControl.Controls.Add(panel); + userControl.Controls.Add(childButton); + + form.Controls.Add(userControl); + form.Controls.Add(okButton); + + form.Show(); + textBox.Focus(); + + Assert.True(textBox.Focused || textBox.ContainsFocus); + + bool processed = form.InvokeProcessMnemonic('O'); + + Assert.True(processed); + Assert.Equal(1, okClickCount); + } + + [WinFormsFact] + public void UserControl_ProcessMnemonic_ButtonBeforeWebBrowser_StillInvokesButtonMnemonic() + { + using TestForm form = new(); + using TestUserControl userControl = new(); + + TextBox textBox = new() + { + TabIndex = 0 + }; + + Button button = new() + { + TabIndex = 7, + Text = "&Button on user control" + }; + + Panel panel = new() + { + TabIndex = 8 + }; + + WebBrowser webBrowser = new() + { + TabIndex = 0, + Url = new Uri("about:blank"), + WebBrowserShortcutsEnabled = false + }; + + int clickCount = 0; + button.Click += (_, _) => clickCount++; + + panel.Controls.Add(webBrowser); + userControl.Controls.Add(textBox); + userControl.Controls.Add(button); + userControl.Controls.Add(panel); + form.Controls.Add(userControl); + + form.Show(); + textBox.Focus(); + + bool processed = userControl.InvokeProcessMnemonic('B'); + + Assert.True(processed); + Assert.Equal(1, clickCount); + } + + [WinFormsFact] + public void WebBrowser_ProcessMnemonic_UnrelatedMnemonic_ReturnsFalse() + { + using TestForm form = new(); + using TestWebBrowser webBrowser = new() + { + Url = new Uri("about:blank"), + WebBrowserShortcutsEnabled = false + }; + + form.Controls.Add(webBrowser); + form.Show(); + webBrowser.CreateControl(); + + bool processed = webBrowser.InvokeProcessMnemonic('B'); + + Assert.False(processed); + } + private class CustomProcessControl : Control { public Func ProcessCmdKeyAction { get; set; } @@ -953,4 +1129,19 @@ public SubWebBrowserBase() : base(ClsidString) public new void SetStyle(ControlStyles flag, bool value) => base.SetStyle(flag, value); } + + private sealed class TestUserControl : UserControl + { + public bool InvokeProcessMnemonic(char charCode) => ProcessMnemonic(charCode); + } + + private sealed class TestForm : Form + { + public bool InvokeProcessMnemonic(char charCode) => ProcessMnemonic(charCode); + } + + private sealed class TestWebBrowser : WebBrowser + { + public bool InvokeProcessMnemonic(char charCode) => ProcessMnemonic(charCode); + } }