Question

I'm creating a tab control with up to 10 tabs. I want to add a Web Browser to each Tab. The user can control how many tabs are displayed. When it save memory by using the same control for each tab or have a seperate control for each tab. Below is what I'm trying to do:

    private System.Windows.Forms.WebBrowser webBrowser1 = null;
    private System.Windows.Forms.WebBrowser webBrowser2 = null;
    private System.Windows.Forms.WebBrowser webBrowser3 = null;

    // Display one tab
    switch (tabCount)
    {
            case 1:
                // tab 1
                tabItem1.Visibility = System.Windows.Visibility.Visible;
                webBrower1 = new WebBrowser();
                webBrowser1.Navigate(txtBoxServerURL.Text));
                // tab 2
                tabItem1.Visibility = System.Windows.Visibility.Collapsed;
                webBrower2 = null;
                break;
                // process remaining tabs and set each webBrowser(2,3,4...) to null
            case 2:
                // tab 1
                tabItem1.Visibility = System.Windows.Visibility.Visible;
                webBrower1 = new WebBrowser();
                webBrowser1.Navigate(txtBoxServerURL.Text));
                // tab 2
                tabItem1.Visibility = System.Windows.Visibility.Visible;
                webBrower2 = new WebBrowser();
                webBrowser2.Navigate(txtBoxServerURL.Text));
                break;
                // tab 3
                tabItem1.Visibility = System.Windows.Visibility.Collapsed;
                webBrower3 = null;
                // process remaining tabs and set each webBrowser(3,4,5...) to null
            // define remaining cases
            default:
                break;
  }

or should I use one WebBrowser Control and one WindowsFormsHost Control for all the tabs:

        for (int i = 0; i < comboCount; i++)
        {
            string title = "Tab Page " + (i + 1);
            TabItem tabItem = new TabItem();
            tabItem.Name = title;
            tabItem.Items.Add(tabPage);  
            System.Windows.Forms.WebBrowser.WebBrowser webBrowser = new System.Windows.Forms.WebBrowser.WebBrowser();
            webBrowser.Navigate(txtBoxServerURL.Text); 
            windowsFormHost.Child = webBrowser;           
        }

Sorry if there's any errors. I wrote this sample without a compile. Also I'm using a System.Windows.Form WebBrowser Control in my WPF application. I had to many inconsistencies getting back the web page title with the WPF WebBrowser control. Hope this all make sense. Thank you.

David

Was it helpful?

Solution

It is much easier to maintain in a for loop. So the second bit of code would be vastly more maintainable.

I would suggest also that you look deeper into WPF and MVVM. A view with a browser control could be reused and instantiated very easily. This pattern allows you to abstract away your visual elements and focus just on the data you want to present, in this case, your web pages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top