Skip to content

Commit 0de0244

Browse files
committed
WinForms - Allow setting of Focus before underlying CEF browser has been initialized
1 parent 530a143 commit 0de0244

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

CefSharp.WinForms/ChromiumWebBrowser.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public partial class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinForm
8686
/// when <see cref="RecreatingHandle"/> is <c>true</c>.
8787
/// </summary>
8888
private Control parkingControl;
89+
/// <summary>
90+
/// This flag is set when the browser gets focus before the underlying CEF browser
91+
/// has been initialized.
92+
/// </summary>
93+
private bool initialFocus;
8994

9095
/// <summary>
9196
/// Gets a value indicating whether this instance is disposed.
@@ -636,6 +641,11 @@ void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
636641
browser.MainFrame.LoadUrl(Address);
637642
}
638643

644+
if(initialFocus)
645+
{
646+
browser.GetHost()?.SetFocus(true);
647+
}
648+
639649
IsBrowserInitializedChanged?.Invoke(this, EventArgs.Empty);
640650
}
641651

@@ -770,16 +780,17 @@ internal void ShowInternal()
770780
ResizeBrowser(Width, Height);
771781
}
772782

773-
/// <summary>
774-
/// Raises the <see cref="E:System.Windows.Forms.Control.GotFocus" /> event.
775-
/// </summary>
776-
/// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
783+
/// <inheritdoc/>
777784
protected override void OnGotFocus(EventArgs e)
778785
{
779786
if (IsBrowserInitialized)
780787
{
781788
browser.GetHost().SetFocus(true);
782789
}
790+
else
791+
{
792+
initialFocus = true;
793+
}
783794

784795
base.OnGotFocus(e);
785796
}

0 commit comments

Comments
 (0)