问题
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