?
Solved

Firing an Event

Posted on 2011-04-21
8
Medium Priority
?
340 Views
Last Modified: 2012-06-21
Hello
I want to fire a custom event from my webbrowser_documentCompleted event.

the webbrowser is held in my Form1 partial class.

Code of documentcompleted event -

                    webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ScrapeScreen);

                    webBrowser.Navigate(buildURL.ToString());

private void ScrapeScreen(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (e.Url.AbsolutePath == (sender as WebBrowser).Url.AbsolutePath)
            {
                if (webBrowser.Document != null)
                {
                    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                    doc.LoadHtml(webBrowser.Document.Body.InnerHtml.ToString());

                    int ranking = ScreenScrape.GetScrapeRanking(txtURL.Text, doc);

                    if (ranking > 0)
                    {
                        txtScrapeRanking.Text = ranking.ToString();
                    }
                    else
                    {
                        want to fire my event here!!!!
                    }
                }
            }
        }

Never done custom events so can someone point me to some help.  Thanks.

Kevin
0
Comment
Question by:KABarrie
  • 3
  • 3
  • 2
8 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 35441328
 else
                    {
                      if(yourCustomEvent != null)
                            yourCustomEvent.Invoke(yourParametersIfAny);
                    }
0
 

Author Comment

by:KABarrie
ID: 35441547

Thanks

Can you point me somewhere that shows me how to define the event?

KEv.
0
 
LVL 22

Accepted Solution

by:
p_davis earned 1000 total points
ID: 35441662
well can you tell me what parameters you need to pass to the method?

if it there aren't any parameters that are special you can.

public event EventHandler yourCustomEvent;

then with that

yourCustomEvent.Invoke(this, new EventArgs());

otherwise you can make your own delegate handler.

public delegate void customDelegateHandler(yourType yourCustomObject);
public event customDelegateHandler yourCustomEvent;

yourCustomEvent.Inoke(instanceOfYourObjectToPass);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 total points
ID: 35441704
Give the event's handler a signature:

public delegate void YourCustomEventHandler(object sender, EventArgs e);

Open in new window


Give the event a name and a handler type:

public event YourCustomEventHandler YourCustomEventName

Open in new window


to end up with:

class YourClassName
{
    public delegate void YourCustomEventHandler(object sender, EventArgs e);
    public event YourCustomEventHandler YourCustomEventName

...

}

Open in new window


Then you can handle the event elsewhere like you would any other event:

class SomeOtherClass
{
    public SomeOtherClass()
    {
        YourCustomObject obj = new YourCustomObject();

        obj.YourCustomEvent += new YourCustomEventHandler(My_Handler);
    }

    private void My_Handler(object sender, EventArgs e)
    {
        // Handle event
    }
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35441708
@p_davis

Sorry. I guess I type too slow  = )
0
 
LVL 22

Expert Comment

by:p_davis
ID: 35441818
not a problem kaufmed -- thanks for the nod
0
 

Author Comment

by:KABarrie
ID: 35446888
Thanks guys!
0
 

Author Closing Comment

by:KABarrie
ID: 35446892
Thanks Again!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question