Skip to content

Commit a058a3c

Browse files
committed
WinForms - Allow setting of Focus before underlying CEF browser has been initialized
1 parent a48cf80 commit a058a3c

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
@@ -76,6 +76,11 @@ public partial class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinForm
7676
/// when <see cref="RecreatingHandle"/> is <c>true</c>.
7777
/// </summary>
7878
private Control parkingControl;
79+
/// <summary>
80+
/// This flag is set when the browser gets focus before the underlying CEF browser
81+
/// has been initialized.
82+
/// </summary>
83+
private bool initialFocus;
7984

8085
/// <summary>
8186
/// Gets a value indicating whether this instance is disposed.
@@ -618,6 +623,11 @@ partial void OnAfterBrowserCreated(IBrowser browser)
618623
browser.MainFrame.LoadUrl(Address);
619624
}
620625

626+
if(initialFocus)
627+
{
628+
browser.GetHost()?.SetFocus(true);
629+
}
630+
621631
IsBrowserInitializedChanged?.Invoke(this, EventArgs.Empty);
622632
}
623633

@@ -752,16 +762,17 @@ internal void ShowInternal()
752762
ResizeBrowser(Width, Height);
753763
}
754764

755-
/// <summary>
756-
/// Raises the <see cref="E:System.Windows.Forms.Control.GotFocus" /> event.
757-
/// </summary>
758-
/// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
765+
/// <inheritdoc/>
759766
protected override void OnGotFocus(EventArgs e)
760767
{
761768
if (IsBrowserInitialized)
762769
{
763770
browser.GetHost().SetFocus(true);
764771
}
772+
else
773+
{
774+
initialFocus = true;
775+
}
765776

766777
base.OnGotFocus(e);
767778
}

0 commit comments

Comments
 (0)