Skip to content

Commit d5d0cb9

Browse files
committed
WinForms Example - Focus browser after loading URL from address bar
- Extend IWinFormsChromiumWebBrowser to make it easier to access CanSelect/Select methods - Update example to call Select() after LoadUrl call Resolves #4965
1 parent 6bceb3b commit d5d0cb9

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CefSharp.WinForms.Example/BrowserTabUserControl.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,11 @@ private void LoadUrl(string url)
486486
Browser.LoadUrl(searchUrl);
487487
}
488488

489+
//Give the browser focus after starting navigation
490+
if (Browser.CanSelect)
491+
{
492+
Browser.Select();
493+
}
489494
}
490495

491496
public async Task HideScrollbarsAsync()

CefSharp.WinForms.Example/Minimal/SimpleBrowserForm.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,19 @@ private void LoadUrl(string url)
177177
{
178178
if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
179179
{
180-
browser.Load(url);
180+
browser.LoadUrl(url);
181+
}
182+
else
183+
{
184+
var searchUrl = "https://www.google.com/search?q=" + Uri.EscapeDataString(url);
185+
186+
browser.LoadUrl(searchUrl);
187+
}
188+
189+
//Give the browser focus after starting navigation
190+
if (browser.CanSelect)
191+
{
192+
browser.Select();
181193
}
182194
}
183195
}

CefSharp.WinForms/IWinFormsChromiumWebBrowser.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,11 @@ public interface IWinFormsChromiumWebBrowser : IChromiumWebBrowserBase, IWin32Wi
3838
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
3939
/// </summary>
4040
event EventHandler IsBrowserInitializedChanged;
41+
42+
/// <inheritdoc cref="Control.CanSelect"/>
43+
bool CanSelect { get; }
44+
45+
/// <inheritdoc cref="Control.Select()"/>
46+
void Select();
4147
}
4248
}

0 commit comments

Comments
 (0)