2014-11-24 58 views
0

我想验证一个winform web浏览器控件的网址,当点击一个按钮时。我想看看网络浏览器的当前网址是否与特定网址匹配。当我尝试运行此代码程序冻结Winforms网页浏览器控件URL验证

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"; 
} 
} 
+0

这是Windows Forms Web浏览器控件还是WPF Web浏览器控件?没有“C#WebBrowser控件”这样的东西 – 2014-11-24 17:04:03

+0

@JohnSaunders Win表格 – SkipDis 2014-11-24 17:11:56

+0

我在你的问题中添加了[tag:winforms]标签,以便Windows窗体专家更有可能看到它。 – 2014-11-24 17:31:49

回答

0

在这里你去。

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)请使用带有URL的模式。

2)使用ToString()作为函数。

+0

不,我需要从网络浏览器控制 – SkipDis 2014-11-24 17:13:27

+0

嗨SkipDis,请检查上面的代码,让我知道如果你需要更多的输入。 – Mandzzz 2014-11-24 18:35:16

+0

嗨,我得到NullReference错误 – SkipDis 2014-11-24 18:42:11