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();
}    
Max
  • 12,622
  • 16
  • 73
  • 101
Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83
  • Where is the webBrowser1 initialization? – FelProNet Mar 12 '14 at 15:26
  • this is initialized in `InitializeComponent()`, which is in Form1() constructor. – Abdur Rahim Mar 12 '14 at 15:28
  • 1
    In addition, this post - http://stackoverflow.com/questions/4269800/webbrowser-control-in-a-new-thread/4271581#4271581 seems to be kind of similar to your issue. Might help you. – FelProNet Mar 12 '14 at 15:28
  • the null object is webbrowser1? – V-SHY Mar 12 '14 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. – Hans Passant Mar 12 '14 at 16:45

1 Answers1

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();
}
V-SHY
  • 3,925
  • 4
  • 31
  • 47
  • `msn.com` is not coming here. – Abdur Rahim Mar 12 '14 at 16:06
  • 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()? – V-SHY Mar 12 '14 at 16:12
  • webBrowser1.Document.Domain' threw an exception of type 'System.Runtime.InteropServices.COMException' – Abdur Rahim Mar 12 '14 at 19:52
  • where ris browser_DocumentCompleted? – Abdur Rahim Mar 13 '14 at 06:03
  • Form1_FormCLosing is not needed. Other Things are ok. Thanks for your cooperation. I have accepted it as a solution. – Abdur Rahim Mar 13 '14 at 10:06
  • Just want to show good habit to dispose item that is disposable at form closing:-) thanks – V-SHY Mar 13 '14 at 10:08
  • While the form is closing, I have debugged that, `webbrowser1` executes it's own dispose method. So I think, it will not be necessary. :-) Welcome. And really thanks to you for your great effort. – Abdur Rahim Mar 13 '14 at 10:11
  • Can you show me how you verify webbrowser execute its own dispose method while form closing in debugging mode? Does it show it's dispose detail to you?thanks:-) – V-SHY Mar 13 '14 at 10:27