[英]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
的构造函数中,每个按钮( wb1
, wb2
和wb3
)创建一个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.