Search code examples
c#winformsgeckofx

GeckoFx 45 Navigate / GoBack / Reload Methods Not Working


I'm using Winforms, C#, and geckofx-45 Windows 32bit 45.0.10 via Nuget (https://www.nuget.org/packages/Geckofx45/) and I'm trying to wire up browser functionality to three buttons for the following:

  • browser.Navigate("www.bing.com");
  • browser.GoBack();
  • browser.Reload();

I'm running this in Visual Studio 2015 and my breakpoints indicate I have no issues with the buttons. The code is called for each of those methods and no changes are reflected in the browser.

To clarify: I have the control added and created in a Form_Load event-bound method. In that method the browser.Navigate, browser.Reload, and browser.GoBack methods work programmatically. However, I cannot get those methods to work anywhere outside of that.

Any ideas? Maybe I'm just doing something incorrectly?

Thank you!

Form1_Load Method Code

        private void Form1_Load(object sender, EventArgs e)
    {
        //Configure Browser
        browser = new GeckoWebBrowser();

        GeckoPreferences.User["places.history.enabled"] = false;
        GeckoPreferences.User["security.warn_viewing_mixed"] = false;
        GeckoPreferences.User["plugin.state.flash"] = 0;
        GeckoPreferences.User["browser.cache.disk.enable"] = false;
        GeckoPreferences.User["browser.cache.memory.enable"] = false;
        GeckoPreferences.User["browser.xul.error_pages.enabled"] = false;
        GeckoPreferences.User["dom.max_script_run_time"] = 0; //let js run as long as it needs to; prevents timeout errors
        GeckoPreferences.User["browser.download.manager.showAlertOnComplete"] = false;
        GeckoPreferences.User["privacy.popups.showBrowserMessage"] = false;
        browser.AllowDrop = false;

        this.Controls.Add(browser);
        browser.Dock = DockStyle.None;
        browser.Size = new System.Drawing.Size(1024, 700);
        browser.Margin = new System.Windows.Forms.Padding(0);
        browser.MinimumSize = new System.Drawing.Size(20, 20);
        int leftOffset = (this.ClientSize.Width - browser.Width) / 2;
        browser.Location = new System.Drawing.Point(leftOffset,2);

        //Hooking browser events
        browser.ShowContextMenu += new EventHandler<GeckoContextMenuEventArgs>(browser_ShowContextMenu); //Strip out undesirable options from the right click menu.
        browser.CreateWindow += new EventHandler<GeckoCreateWindowEventArgs>(browser_CreateWindow); //Prevent the browser from opening popup windows in new windows / links in new windows.
        browser.NSSError += new EventHandler<GeckoNSSErrorEventArgs>(browser_NSSError); //Bypass SSL certificate issues
        browser.NavigationError += new EventHandler<GeckoNavigationErrorEventArgs>(browser_NavigationError); //If there are any issues encountered during page loads
        browser.Navigated += new EventHandler<GeckoNavigatedEventArgs>(browser_Navigated); //React appropriately to URL navigation

        //Navigate the browser as desired.
        browser.Navigate(defaultHomeWebsite);
    }

Form Buttons that are fired but the browser methods don't Work

    /// <summary>
    /// Browser Button: Go Home
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button4_Click(object sender, EventArgs e)
    {
        browser.Navigate("www.bing.com");
    }

    /// <summary>
    /// Browser Button: Go Back
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button3_Click(object sender, EventArgs e)
    {
        browser.GoBack();
    }

    /// <summary>
    /// Browser Button: Refresh Page
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button2_Click(object sender, EventArgs e)
    {
        browser.Reload();
    }

Solution

  • @Tom who commented gave me a good clue to check out.

    This is not an issue with GeckoFx -- I initially solved this by moving my browser initialization and settings code to my Form1() constructor. This worked and I've been scratching my head for a few hours as to 'why' it worked. It turns out that I was accidentally double binding my Form_Load method and thus, two instances of "browser" were being created during runtime.

    The solution was to remove one of the event bindings for Form_Load.