Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 906
Proximo Avatar asked Sep 14 '25 19:09

Proximo


1 Answers

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).

like image 128
Matt Brindley Avatar answered Sep 16 '25 08:09

Matt Brindley