Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Event Handling in C# 2.0

Event Handling in C# 2.0

Scheduled Pinned Locked Moved C#
csharptestingbeta-testinghelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    A Asif
    wrote on last edited by
    #1

    Hi All, I am working on a project that requires handling of custom events. Some class will raise the DataEvent of RaiseEventClass. I have to implement event handler for the DataEvent in the RaiseEventClass. Please help me how to implement event handler. Please show some codes if possible. Below is the skeleton of the class. Thanks in advance! public class RaiseEventClass { public delegate void RaiseEventDelegate(ArrayList files); public event RaiseEventDelegate DataEvent; public void SearchFiles(ArrayList list) { DataEvent(list); //raising event for testing } }

    A.Asif

    Mircea PuiuM C 2 Replies Last reply
    0
    • A A Asif

      Hi All, I am working on a project that requires handling of custom events. Some class will raise the DataEvent of RaiseEventClass. I have to implement event handler for the DataEvent in the RaiseEventClass. Please help me how to implement event handler. Please show some codes if possible. Below is the skeleton of the class. Thanks in advance! public class RaiseEventClass { public delegate void RaiseEventDelegate(ArrayList files); public event RaiseEventDelegate DataEvent; public void SearchFiles(ArrayList list) { DataEvent(list); //raising event for testing } }

      A.Asif

      Mircea PuiuM Offline
      Mircea PuiuM Offline
      Mircea Puiu
      wrote on last edited by
      #2

      First learn, then code :-) You could start here[^] or here at CP[^].

      SkyWalker

      1 Reply Last reply
      0
      • A A Asif

        Hi All, I am working on a project that requires handling of custom events. Some class will raise the DataEvent of RaiseEventClass. I have to implement event handler for the DataEvent in the RaiseEventClass. Please help me how to implement event handler. Please show some codes if possible. Below is the skeleton of the class. Thanks in advance! public class RaiseEventClass { public delegate void RaiseEventDelegate(ArrayList files); public event RaiseEventDelegate DataEvent; public void SearchFiles(ArrayList list) { DataEvent(list); //raising event for testing } }

        A.Asif

        C Offline
        C Offline
        CKnig
        wrote on last edited by
        #3

        First you should change

        A.Asif wrote:

        public void SearchFiles(ArrayList list) { DataEvent(list); //raising event for testing }

        to public void SearchFiles(ArrayList list) { if (DataEvent != null) DataEvent(list); //raising event for testing } just to handle the case when no handler is attached to the event. Then if you've got an RaiseEventClass object (call it "myObj") and want to handle the event just use myObj.DataEvent += new RaiseEventDelegate(myHandler ) where myHandler is a function like void myHandler(ArrayList files) { // TODO: whatever you want } By the way: afert typing "+= new" in the line where you attach the event you can hit twice and VS will write a handler-function for you (so you don't have to check the delegate to know exactly what kind of parameter / return you have to use) One other comment: you should include a "sender" parameter in every event you write (like object sender, or RaseEventClass sender - this helps you handle events from many sources in the same handler function and is always a good pattern.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups