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. Replicate CancelEventArgs behaviour?

Replicate CancelEventArgs behaviour?

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 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.
  • M Offline
    M Offline
    misterbear
    wrote on last edited by
    #1

    How can one replicate the Close() event with its corresponding CancelEventArgs? I.e. how to find out from the code firing the event what the receiver set this attribute to? Does anyone know and can help clarify this procedure to me...

    M 1 Reply Last reply
    0
    • M misterbear

      How can one replicate the Close() event with its corresponding CancelEventArgs? I.e. how to find out from the code firing the event what the receiver set this attribute to? Does anyone know and can help clarify this procedure to me...

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      If there's just a single event handler attached, then it's fairly easy: In your class you need: public delegate MyCancelHandler(bool cancel); public event MyCancelHandler AnEventThatCanBeCancelled; // Later on when you want the event to fire if (AnEventThatCanBeCancelled != null) { bool cancel = false; AnEventThatCanBeCancelled(cancel); if (cancel) { MessageBox.Show("Event has been cancelled by a listener"); } else { MessageBox.Show("Event has NOT been cancelled, go on with whatever you want"); } } If there's more than one listener attached, then you'll only get the cancel info of the last listener notified. In this case you'll have to go through all registered delegates (using MulticastDelegate.GetInvocationList()) and check all of them if anyone wants to cancel your event. Regards, mav

      H 1 Reply Last reply
      0
      • M mav northwind

        If there's just a single event handler attached, then it's fairly easy: In your class you need: public delegate MyCancelHandler(bool cancel); public event MyCancelHandler AnEventThatCanBeCancelled; // Later on when you want the event to fire if (AnEventThatCanBeCancelled != null) { bool cancel = false; AnEventThatCanBeCancelled(cancel); if (cancel) { MessageBox.Show("Event has been cancelled by a listener"); } else { MessageBox.Show("Event has NOT been cancelled, go on with whatever you want"); } } If there's more than one listener attached, then you'll only get the cancel info of the last listener notified. In this case you'll have to go through all registered delegates (using MulticastDelegate.GetInvocationList()) and check all of them if anyone wants to cancel your event. Regards, mav

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        That won't work. bool (System.Boolean) is a value type, and you'd need to declare the delegate parameter as out or ref. Besides, it's far easier to just reuse CancelEventHandler. Since it uses the CancelEventArgs which has a Cancel property (that will keep the value), you don't need to declare your own event. Also, while it's not required, events should typically follow the EventHandler signature and the second parameter should (agian, not required to) derive from EventArgs. CancelEventArgs does, of course, extend this class. If you want to know what a specific callee of the callback set the CancelEventArgs.Cancel property to, your past paragraph is correct.

        Microsoft MVP, Visual C# My Articles

        M 1 Reply Last reply
        0
        • H Heath Stewart

          That won't work. bool (System.Boolean) is a value type, and you'd need to declare the delegate parameter as out or ref. Besides, it's far easier to just reuse CancelEventHandler. Since it uses the CancelEventArgs which has a Cancel property (that will keep the value), you don't need to declare your own event. Also, while it's not required, events should typically follow the EventHandler signature and the second parameter should (agian, not required to) derive from EventArgs. CancelEventArgs does, of course, extend this class. If you want to know what a specific callee of the callback set the CancelEventArgs.Cancel property to, your past paragraph is correct.

          Microsoft MVP, Visual C# My Articles

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          Of course you're right, Heath.:doh: This is what you get when you don't actually try out your own suggestions...

          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