Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
if a have an eventHandlar shall i call it from If condition

C#
if (instance.NavigateError += new SHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(instance_NavigateError))


but its not working, actually i need to get response whether its calling or not?

please help if u have any other idea.



C#
private void button6_Click(object sender, EventArgs e)
        {

            

            SHDocVw.WebBrowser instance = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
            webBrowser1.Navigate("http://www.g4ogle.com/");
            

            instance.NavigateError += new SHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(instance_NavigateError);
            string asj = instance.StatusBar.GetType().ToString();
         
           
            
            
        }
        void instance_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
        {

            string filename = textBox1.Text;

            string fileName = filename.Substring(0, filename.LastIndexOf((".")));
            fileName += ".txt";
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            //  string asg = Ret[0].ToString();
            int name = Convert.ToInt32(StatusCode);
            //ret1.Add("----" + "The URL '" + url + "' is Not valid.");
            // MessageBox.Show("error on page");

            Ret.Add("----" + "The URL '" + URL + "' is Not valid.");
            //File.AppendAllLines(fileName, Ret);
            MessageBox.Show("Completed......", "Info Box", MessageBoxButtons.OK, MessageBoxIcon.Information);



        }
Posted
Updated 12-Mar-14 0:17am
v2
Comments
Vedat Ozan Oner 12-Mar-14 5:15am    
why don't you use http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser%28v=vs.110%29.aspx

No, you don't do that.

There is no point in testing the value of a event handler addition: it will never be false because it does not return a bool value. It doesn't return any value! So all you will get is a compiler error about not being able to convert a void to a bool...

And you don't "call" event handlers - you raise the event in response to some stimuli, usually a user input of some kind, and the system calls all the event handler methods that are registered for that event for you.

I think that you are a little confused as to what events are - perhaps it would be a good idea to read back through your lecture notes and think about it a little more before you try leaping into code!
 
Share this answer
 
Comments
Ashwani Gusain 12-Mar-14 5:43am    
Thanks Griff but sometime its calling but not every time as i checked in my code, you are right it always true, but why its calling some or not calling sometime?
OriginalGriff 12-Mar-14 5:46am    
Show us code that compiles, and we might be able to help...
Ashwani Gusain 12-Mar-14 6:18am    
hi griff please check updated code.
OriginalGriff 12-Mar-14 6:39am    
That code is pretty much wrong: you are adding an event handler every time the user presses the button! And...you add it after the Navigate method is called, so it is quite possible that the error will have occurred before the handler is added, so the method will not be called every time there is a problem.

You only need to add the handler once: preferably when the WebBrowser instance is created. So if you are adding it to you form at design time, then add it in the Events panel of the Properties pane for the WebBrowser. If you manually create the instance and add it to your form, then add the handler at that point, not on the button click.

Hopefully, doing that will get rid of your problem!
Ashwani Gusain 12-Mar-14 6:55am    
Hi Griff, as i mentioned earlier that code is working fine but the main concern is whenever that navigate url got wrong url it will call to instance_Navigateerror function. but my question is after calling from "instance.NavigateError += new SHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(instance_NavigateError);" , it is possible to get any clue about it will call or not?
If you need something to happen whenever the event handler is called, then include it in the event handler.

If you want to call the event handler regardles of whether the related event is raised or not, you can do that like you would call every other method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900