How to get the url of a WebBrowser control
if (webBrowser1.Url.AbsoluteUri == "www.google.com")
{
label9.Text = webBrowser1.Url.AbsoluteUri;
}
This is my current code. When I click the button to run, I get an error.
An object reference is not set on an object instance.
And I don't know why he does it or how to fix it. Any help would be great.
It also needs to run on a timer so that it can be checked.
source to share
The property Url
will remain null
until the control is rendered, so use this:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
if (webBrowser1.Url.ToString() == "www.google.com") {
label9.Text = webBrowser1.Url.ToString();
}
}
And in your button Click
event add:
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
source to share
I thought id was a comment on this, I literally took your
"webBrowser1.Url.AbsoluteUri;"
and in my case i am using combotextbox so double click your browser form and it will take you to the even handler, i will just put
"combobox1.text= webBrowser1.Url.AbsoluteUri;"
and now it works for me. You got me in time, but all you need to check is check on combobox1.text or whatever you use for your url.
source to share