How to change the URL using CefSharp WinForms

Kraang Prime picture Kraang Prime · May 24, 2015 · Viewed 31.2k times · Source

So, I decided to give CefSharp another go, grabbed the CefSharp.Winforms nuget, and dropped in the following code :

    public CefSharp.WinForms.ChromiumWebBrowser browser;

    public Form1() {
        InitializeComponent();

        browser=new CefSharp.WinForms.ChromiumWebBrowser( "http://www.imdb.com" ) {
            Dock=DockStyle.Fill,
        };
        tabPage2.Controls.Add( browser );
    }

... which works. It creates the webbrowser control, and loads the page (YAY !!). Now, what I want to do, is based on a users selection in a ListView, I want to change the page from http://www.imdb.com to something else. Essentially looking for a way to do the same thing that WebBrowser.Navigate( ... ) from the IE Browser component, but in CefSharp.WinForms.

Seems rather silly (and pointless), if there is no way to change the URL after the browser is initialized, so on that, logically, there must be a way.

browser.Address is as close as I can find within the component itself, but it is a readonly property.

Thanks in advance for any assistance with this matter.

Answer

Kraang Prime picture Kraang Prime · Jun 17, 2015

As pointed out by Majed DH in the comments, the correct solution to this precise and very clear question is:

There is a ChromiumWebBrowser.Load(string url) in WPF version . i think it may be there in winform version too. – Majed DH May 24 at 10:29

More specifically, a code example on how this is done, is as follows:

public CefSharp.WinForms.ChromiumWebBrowser browser;

public Form1() {
    InitializeComponent();

    browser=new CefSharp.WinForms.ChromiumWebBrowser( "http://www.imdb.com" ) {
        Dock=DockStyle.Fill,
    };
    this.Controls.Add( browser );

    // Simply pass the URL you wish to navigate to, to the 'Load' method
    browser.Load( "http://www.google.ca" );
}

In CefSharp, the functionally equivalent method to the native WebBrowser controls' Navigate method, is Load.

Footnote: Upon further research, there is no clear indication as to why the developers of the CefSharp project chose to use Load when Navigate more accurately describes the action and is also more consistent with the built-in WebBrowser control's method.