1

I am trying to make a simple browser in Windows Form Application. My first site, "http://www.msn.com" is loaded in browser on Form_Load. but when I set a valid url in textbox1, nothing comes up. and webBrowser1.Document.Focus(); catch an error. "Object reference is null or ....", that common problem of null instance.

private void Form1_Load(object sender, EventArgs e)
{          
    myMethod("http://www.msn.com"); 
}

private void button1_Click(object sender, EventArgs e)
{
    myMethod(textbox1.Text);
}

public void myMethod(string url)
{
    webBrowser1.DocumentCompleted += browser_DocumentCompleted;
    webBrowser1.Navigate(new Uri(url));
    webBrowser1.Document.Focus();
}    
5
  • Where is the webBrowser1 initialization? Commented Mar 12, 2014 at 15:26
  • this is initialized in InitializeComponent(), which is in Form1() constructor. Commented Mar 12, 2014 at 15:28
  • 1
    In addition, this post - stackoverflow.com/questions/4269800/… seems to be kind of similar to your issue. Might help you. Commented Mar 12, 2014 at 15:28
  • the null object is webbrowser1? Commented Mar 12, 2014 at 16:26
  • 1
    You cannot use the Document property until the DocumentCompleted event fires. Using webBrowser1.Focus() ought to be sufficient. Subscribing the event again in the method is wrong as well, that needs to be done only once. It belongs in the constructor, trivially done right with the designer. Commented Mar 12, 2014 at 16:45

1 Answer 1

1

Latest

Refer to @Hans Passant, I finally understand what part of the code is incorrect.

We subscribe from event in myMethod

webBrowser1.DocumentCompleted += browser_DocumentCompleted;

and we never Unsubscribe from Event

Therefore we will increase the subscribe times and trigger the browser_DocumentCompleted more than one time if webBrowser1.DocumentCompleted happens

Suggestions

//constructor
public Form1()
{
    InitializeComponent();
    //declare webBrowser1 before this
    //subscribe only once here
    webBrowser1.DocumentCompleted += browser_DocumentCompleted;
    //try these two if still fail
    //this.webBrowser1.AllowWebBrowserDrop = false; 
    //this.webBrowser1.ScrollBarsEnabled = false;
}

private void Form1_Load(object sender, EventArgs e)
{          
    myMethod("http://www.msn.com"); 
}

private void button1_Click(object sender, EventArgs e)
{
    myMethod(textbox1.Text);
}

public void myMethod(string url)
{
    webBrowser1.Navigate(new Uri(url));
    webBrowser1.Document.Focus();
}

private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    this.Text = e.Url.ToString() + " loaded";
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    //unsubscribe here
    webBrowser1.DocumentCompleted -= browser_DocumentCompleted;
    webBrowser1.Dispose();
}
Sign up to request clarification or add additional context in comments.

8 Comments

msn.com is not coming here.
for me I can, but the title display very weird because my Absolute Uri becomes very long and weird. Did you put breakpoint to check what happen when you doing webBrowser1.Navigate()?
webBrowser1.Document.Domain' threw an exception of type 'System.Runtime.InteropServices.COMException'
where ris browser_DocumentCompleted?
Form1_FormCLosing is not needed. Other Things are ok. Thanks for your cooperation. I have accepted it as a solution.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.