0

I am using CefSharp to create a browser. It is working, I can navigate to various websites by using new tab. But when I click on previous tabs, all the tab shows same URL in the address bar and all of them has exactly same title. Here is my code:

    private void FormBrowser_Load(object sender, EventArgs e)
    {
        CefSettings settings = new CefSettings();
        Cef.Initialize(settings);
        ChromiumWebBrowser browser = new ChromiumWebBrowser(toolStripTextBoxAddress.Text);
        browser.Parent = tabControl.SelectedTab;
        browser.Dock = DockStyle.Fill;
        browser.AddressChanged += Browser_AddressChanged;
        browser.TitleChanged += Browser_TitleChanged;
    }

    // new tab function
    public void addNewTab()
    {
        TabPage tpage = new TabPage();
        tpage.Text = "New Tab";
        tabControl.Controls.Add(tpage);
        tabControl.SelectTab(tabControl.TabCount - 1);
        toolStripTextBoxAddress.Text = "";
        ChromiumWebBrowser browser = new ChromiumWebBrowser(toolStripTextBoxAddress.Text);
        browser.Parent = tpage;
        browser.Dock = DockStyle.Fill;
        browser.AddressChanged += Browser_AddressChanged;
        browser.TitleChanged += Browser_TitleChanged;
    }

    private void Browser_TitleChanged(object sender, TitleChangedEventArgs e)
    {
        this.Invoke(new MethodInvoker(() =>
        {
            tabControl.SelectedTab.Text = e.Title;
        }));
    }

    private void Browser_AddressChanged(object sender, AddressChangedEventArgs e)
    {
        this.Invoke(new MethodInvoker(() =>
        {
            toolStripTextBoxAddress.Text = e.Address;
        }));
    }

    // navigate method
    private void toolStripTextBoxAddress_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            if (!string.IsNullOrEmpty(toolStripTextBoxAddress.Text))
            {
                if (!toolStripTextBoxAddress.Text.Contains("."))
                {
                    getCurrentBrowser().Load("http://www.google.com/search?q=" + toolStripTextBoxAddress.Text);
                }
                else
                {
                    getCurrentBrowser().Load(toolStripTextBoxAddress.Text);
                }
            }
        }
    }

   // get current browser
    private ChromiumWebBrowser getCurrentBrowser()
    {
        return (ChromiumWebBrowser)tabControl.SelectedTab.Controls[0];
    }

    // new tab button
    private void toolStripButtonNewTab_Click(object sender, EventArgs e)
    {
        addNewTab();
    }

Here is what I have tried:

    private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChromiumWebBrowser currentBrowser = getCurrentBrowser();
        toolStripTextBoxAddress.Text = currentBrowser.Address;
    }

When i try to open a new tab it is giving me an error in this line return (ChromiumWebBrowser)tabControl.SelectedTab.Controls[0];

How can I solve this problem? Thanks in advance.

Rubel Hosen
  • 199
  • 4
  • 20

1 Answers1

0

I wrote my multi-tab cefsharp code in a very similar fashion to yours, and encountered the same error.

It was cause by the default number of tab pages. (When you drag tabcontrol to your form, it by default comes with 2 tabpages to start with). From the property panel, I removed those two tabpages, so that the browser starts wit zero tabpage. Any tabpage is only added when you start browsing, by inputting url or clicking favorites.

If you do not set the initial number of tabpages to zero, those two "empty" tabpages have no browser attached to them. Therefore the getcurrentbrowser() function fails to find any browser on those empty tabpages and errors occur.