简体   繁体   中英

Winforms Webbrowser control URL Validation

I am trying to validate a winform web browser control url when a button is clicked. I would like to see if the web browser's current url matches a certain url. When I try to run this code the program freezes

private void button_Click(object sender, EventArgs e)
 {
    // Check to see if web browser is at URL
    if (webBrowser1.Url.ToString != "www.google.com" || webBrowser1.Url.ToString == null)
 { 
    // Goto webpage
    webBrowser1.Url = new Uri("www.google.ca");
 }
 else {
    webBrowser1.Document.GetElementById("first").InnerText = "blah";
    webBrowser1.Document.GetElementById("second").InnerText = "blah";
 }
}

Here you go.

private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri("https://www.google.ca");

            // Check to see if web browser is at URL
            if (webBrowser1.Url != null)
            {
                if (webBrowser1.Url.ToString() != "https://www.google.com" || webBrowser1.Url.ToString() == null)
                {
                    // Goto webpage
                    webBrowser1.Url = new Uri("www.google.ca");
                }
                else
                {
                    webBrowser1.Document.GetElementById("first").InnerText = "blah";
                    webBrowser1.Document.GetElementById("second").InnerText = "blah";
                }
            }

        }

1) Please use the schema with the URL.

2) Use ToString() as a function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM