简体   繁体   English

如何在正在运行的Forms应用程序中动态更改WebBrowser对象?

[英]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 . 为了实现这一点,我在Form定义了一个WebBrowser对象。 This WebBrowser is called webBrowser1 . WebBrowser称为webBrowser1 In the constructor of the Form I create one WebBrowser object per button ( wb1 , wb2 and wb3 ). Form的构造函数中,每个按钮( wb1wb2wb3 )创建一个WebBrowser对象。 Then, I use the method Navigate on each of the objects to preload the webpages. 然后,我在每个对象上使用Navigate方法来预加载网页。 In the onClick handlers, I reassign webBrowser1 to one of the objects created in the constructor. 在onClick处理程序中,我将webBrowser1重新分配给在构造函数中创建的对象之一。

The problem is that I can't visualize the WebBrowser after the reassignment. 问题是重新分配后我无法看到WebBrowser

This is the class that implements the Form: 这是实现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? 编辑1: “预加载”网页是什么意思?

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. 我不知道确切的技术术语来描述这一点,但是我正在寻找的效果等同于Web浏览器中的选项卡。 For example, let's say that I'm browsing the web with Mozilla Firefox and I have three tabs open. 例如,假设我正在使用Mozilla Firefox浏览网络,并且打开了三个选项卡。 The first tab has the Google homepage loaded, the second one has the stackoverflow homepage and the third one Wikipedia. 第一个选项卡已加载Google主页,第二个选项卡具有stackoverflow主页,第三个选项卡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. 正如问题注释中指出的那样,我必须管理表单的Controls集合。 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. 我还复制了Visual Studio在构造函数中生成的一些代码,以使所有WebBrowser对象具有相同的显示功能。 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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows窗体应用程序和WebBrowser控件之间进行通信 - How can I communicate between a windows forms application and a WebBrowser control 如何将此Web浏览器请求更改为HTTPWebRequest? - How can I change this webbrowser request to a HTTPWebRequest? 如何使用按钮更改WebBrowser URL? - How can I change a WebBrowser URL with a button? 如何在表单应用程序中动态更改快捷键? - How can I change shortcut keys dynamically in form application? 如何在Windows Forms Application的所有选项卡中更改控件的属性? - How can I change the properties of controls in all tabs in Windows Forms Application? 如何在Windows Forms应用程序的所有控件中更改FontFamily属性? - How can I change the FontFamily Property in all Controls in Windows Forms Application? 如何在 C# Windows 窗体应用程序中修复窗体大小而不让用户更改其大小? - How can I fix the form size in a C# Windows Forms application and not to let user change its size? 对于使用“ DataGridView”的Windows窗体应用程序,如何检查数据源中的值并更改单独单元格的颜色? - For a Windows Forms Application using `DataGridView`, how can I check a value in my DataSource and change the color of a separate cell? 如何在Windows Forms应用程序中更改所有屏幕的字体大小 - How can I change the font size of all the screens in a windows forms application 如何在动态创建的按钮上更改动态创建的 label 文本单击 C# Windows Z6450242531912981C368Z 应用程序3CAE86 - How to change dynamically created label text on dynamically created button click in C# Windows Forms application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM