Answered by:
Display browser by pressing the button

Question
-
How to make the project WinForms by pressing a button opens a separate window - webbrowser?
I did so:
private void button2_Click(object sender, EventArgs e) { webbrowseform.Show(); //A separate window WinForms , it contains an element WebBrowser. }
Shows the exception ObjectDisposedException , how to fix the exception, that if you press the button it did not show?- Edited by Stenl1 Monday, January 23, 2012 8:39 PM
Answers
-
Hello Stenl1,
How to make the project WinForms by pressing a button opens a separate window - webbrowser?
I did so:
Shows the exception ObjectDisposedException , how to fix the exception, that if you press the button it did not show?
private void button2_Click(object sender, EventArgs e) { webbrowseform.Show(); //<span id="x_result_box" lang="en"><span>A separate window</span> <span>WinForms , </span></span><span id="x_result_box" lang="en"><span>it contains</span> <span>an element WebBrowser.</span></span> }
probably the form and null, so you get the exception, so before you open it sure is not null, Diver's create an 'instance and then to display it
wb = New WebBrowserForm ();
wb.Show ();
Regards.
- Edited by Carmelo La Monica Monday, January 23, 2012 9:11 PM
- Proposed as answer by TSoftware-Old Friday, January 27, 2012 2:16 AM
- Marked as answer by Lie You Friday, January 27, 2012 5:00 AM
-
Give this a shot:
private void button2_Click(object sender, EventArgs e) { WebBrowserForm l_form = null; try { l_form = new WeBrowserForm(); l_form.ShowDialog(); } finally { if (l_form != null) { l_form.Dispose(); } } }
This will show the form modally, which will stop the execution of your calling form until it completes.
It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.- Proposed as answer by TSoftware-Old Friday, January 27, 2012 2:16 AM
- Marked as answer by Lie You Friday, January 27, 2012 5:00 AM
-
The web browser control is disposed once the Form is closed. You need to create a new instance of WebBrowserForm() each time the button is clicked, rather than keeping a reference to it around. Try this instead:
private void button2_Click(object sender, EventArgs e) { var form = new WebBrowseForm(); // Set any properties you need form.Show(); }
Then get rid of your webbrowseform variable - keeping that reference around won't help you much.
Check out My Blog. Now updated to actually work!- Proposed as answer by TSoftware-Old Friday, January 27, 2012 2:17 AM
- Marked as answer by Lie You Friday, January 27, 2012 5:00 AM
All replies
-
You need to post a lot more details than what you're showing. The exception means that webbrowseform, or some class it relies on, has already been disposed, so it can't be shown.
We can't really help you unless you can show us the code where webbrowseform is initialized and tell us what type it is, and show us other code which is referencing it. Does this happen every time the button is clicked, or does the browser work correctly the first time then throw exceptions each time after that?
Check out My Blog. Now updated to actually work! -
Hello Stenl1,
How to make the project WinForms by pressing a button opens a separate window - webbrowser?
I did so:
Shows the exception ObjectDisposedException , how to fix the exception, that if you press the button it did not show?
private void button2_Click(object sender, EventArgs e) { webbrowseform.Show(); //<span id="x_result_box" lang="en"><span>A separate window</span> <span>WinForms , </span></span><span id="x_result_box" lang="en"><span>it contains</span> <span>an element WebBrowser.</span></span> }
probably the form and null, so you get the exception, so before you open it sure is not null, Diver's create an 'instance and then to display it
wb = New WebBrowserForm ();
wb.Show ();
Regards.
- Edited by Carmelo La Monica Monday, January 23, 2012 9:11 PM
- Proposed as answer by TSoftware-Old Friday, January 27, 2012 2:16 AM
- Marked as answer by Lie You Friday, January 27, 2012 5:00 AM
-
for the first time the window opens good. And when you close it and press the open, there is an ObjectDisposedException
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MosBot { public partial class WebBrowserForm : Form { public WebBrowserForm() { InitializeComponent(); } } }
-
Give this a shot:
private void button2_Click(object sender, EventArgs e) { WebBrowserForm l_form = null; try { l_form = new WeBrowserForm(); l_form.ShowDialog(); } finally { if (l_form != null) { l_form.Dispose(); } } }
This will show the form modally, which will stop the execution of your calling form until it completes.
It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.- Proposed as answer by TSoftware-Old Friday, January 27, 2012 2:16 AM
- Marked as answer by Lie You Friday, January 27, 2012 5:00 AM
-
The web browser control is disposed once the Form is closed. You need to create a new instance of WebBrowserForm() each time the button is clicked, rather than keeping a reference to it around. Try this instead:
private void button2_Click(object sender, EventArgs e) { var form = new WebBrowseForm(); // Set any properties you need form.Show(); }
Then get rid of your webbrowseform variable - keeping that reference around won't help you much.
Check out My Blog. Now updated to actually work!- Proposed as answer by TSoftware-Old Friday, January 27, 2012 2:17 AM
- Marked as answer by Lie You Friday, January 27, 2012 5:00 AM