简体   繁体   中英

How can I change a WebBrowser object dynamically in a running Forms application?

I'm building an app that preloads webpages when it's run. Then, if the user does some actions (eg clicking a button) the program shows in a panel the preloaded webpage.

In order to achieve this, I have defined a WebBrowser object in the Form . This WebBrowser is called webBrowser1 . In the constructor of the Form I create one WebBrowser object per button ( wb1 , wb2 and wb3 ). Then, I use the method Navigate on each of the objects to preload the webpages. In the onClick handlers, I reassign webBrowser1 to one of the objects created in the constructor.

The problem is that I can't visualize the WebBrowser after the reassignment.

This is the class that implements the Form:

public partial class Form1 : Form
{
    private WebBrowser wb1;
    private WebBrowser wb2;
    private WebBrowser wb3;

    public Form1()
    {
        wb1 = new WebBrowser();
        wb1.Navigate("https://www.google.com/");
        wb2 = new WebBrowser();
        wb2.Navigate("https://stackoverflow.com/");
        wb3 = new WebBrowser();
        wb3.Navigate("https://en.wikipedia.org/wiki/Main_Page");
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1 = wb1;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        webBrowser1 = wb2;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        webBrowser1 = wb3;
    }
}

What am I doing wrong?

Edit 1: What do I mean by "preload" the webpages?

I don't know the exact technical term to describe this, but the effect I'm looking for is equivalent to the tabs in a web browser. For example, let's say that I'm browsing the web with Mozilla Firefox and I have three tabs open. The first tab has the Google homepage loaded, the second one has the stackoverflow homepage and the third one Wikipedia. When I switch tabs there isn't a reloading of the webpage (not even from cache), it just brings whichever webpage is loaded in the tab to the foreground.

As it was pointed out in the question comments I have to manage the Controls collection of the form. I also copied some of the code generated by visual studio in the constructor to make all the WebBrowser objects have the same display features. I'm not sure if this last point is necessary.

Here's the code:

public partial class Form1 : Form
{
    private WebBrowser wb1;
    private WebBrowser wb2;
    private WebBrowser wb3;

    public Form1()
    {
        wb1 = new WebBrowser();
        wb1.Navigate("https://www.google.com/");
        wb2 = new WebBrowser();
        wb2.Navigate("https://stackoverflow.com/");
        wb3 = new WebBrowser();
        wb3.Navigate("https://en.wikipedia.org/wiki/Main_Page");
        List<WebBrowser> lwb = new List<WebBrowser>();
        lwb.Add(wb1);
        lwb.Add(wb2);
        lwb.Add(wb3);
        foreach (WebBrowser wb in lwb)
        {
            wb.Dock = System.Windows.Forms.DockStyle.Fill;
            wb.Location = new System.Drawing.Point(0, 0);
            wb.MinimumSize = new System.Drawing.Size(20, 20);
            wb.Name = "webBrowser1";
            wb.Size = new System.Drawing.Size(260, 208);
            wb.TabIndex = 0;
        }
        InitializeComponent();
    }

    private void bringToFront(WebBrowser wb)
    {
        this.panel1.Controls.Remove(this.webBrowser1);
        this.webBrowser1 = wb;
        this.panel1.Controls.Add(this.webBrowser1);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        bringToFront(wb1);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        bringToFront(wb2);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        bringToFront(wb3);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM