Is there a way to dynamically create and dispose of Webbrowser control?

血红的双手。 提交于 2019-12-05 05:16:37

问题


I have this App that uses the Webbrowser control to do automated browsing. I need to come up with a way to automatically close the browser (dispose), then create another instance that actually works.

Here's some of the code that I have so far.

this.webBrowser2 = new System.Windows.Forms.WebBrowser();
this.webBrowser2.Dock = System.Windows.Forms.DockStyle.Bottom;
this.webBrowser2.Location = new System.Drawing.Point(0, 34);
this.webBrowser2.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser2.Name = "webBrowser2";
this.webBrowser2.ScriptErrorsSuppressed = true;
this.webBrowser2.Size = new System.Drawing.Size(616, 447);
this.webBrowser2.TabIndex = 1;

So I was thinking if I dispose of the webbrower instance.

webBrowser2.dispose();

And then creating a new instance of the webbrowser object.

WebBrowser w = new WebBroswer();
w.Navigate(url);

Unfortunately this doesn't work. The new instance of the browser doesn't show up and the disposed browser object just stays frozen in the windows form.

Is there something I am doing wrong?

Thanks


回答1:


You need to add and remove the WebBrowsers from the form's Controls property:

this.Controls.Remove(webBrowser2);
this.Controls.Add(w);

If you get stuck, there's also this article that has an almost complete walkthrough to adding and removing controls (it doesn't include much about events).



来源:https://stackoverflow.com/questions/937135/is-there-a-way-to-dynamically-create-and-dispose-of-webbrowser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!