WebBrowserNavigatingEventArgs.Cancel not working on WebBrowser.Navigating?
I have a WebBrowser, a check box, a button and the following code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
开发者_JS百科 }
void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if(checkBox1.Checked) e.Cancel = true;
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com/#hl=en&q=hi&aq=f&aqi=&aql=&oq=&gs_rfai=&fp=9cebbf5a0cce78cd");
}
}
So, after clicking button1 and going to the Google results I check the checkBox1 so that it should cancel. It works for some links, but it doesn't cancel navigation when I click on "Next" or "Previous" or any of the results pages at the bottom. You can test it yourself. Here's the documentation for the WebBrowserNavigatingEventArgs. The question is what's wrong with my code and how can I get the WebBrowser to cancel navigation for every link. Thank you.
Assuming the event handler is correctly hooked up that should work (it certainly did for me).
First thing to do is to ensure webBrowser1_Navigating
is being called -- put a break point on it.
精彩评论