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. Cancel Events

Cancel Events

Scheduled Pinned Locked Moved C#
question
6 Posts 4 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.
  • B Offline
    B Offline
    Beringer
    wrote on last edited by
    #1

    I've been working with my own custom events for controls and thought I had it figured out until I wanted to implement "Before" events(you know like BeforeExpand in TreeView). This kind of event will have a "Cancel" or "Handled" flag to indicate to the sender that the event was canceled (or handled) by the subscriber to the event. My question is, as the sender of the event, how do I know about the Canceling or handling? I understand that the eventargs object will have a boolean value to indicate this but how does that information get back to the sender? Thanks in advance, Eric

    L J M 3 Replies Last reply
    0
    • B Beringer

      I've been working with my own custom events for controls and thought I had it figured out until I wanted to implement "Before" events(you know like BeforeExpand in TreeView). This kind of event will have a "Cancel" or "Handled" flag to indicate to the sender that the event was canceled (or handled) by the subscriber to the event. My question is, as the sender of the event, how do I know about the Canceling or handling? I understand that the eventargs object will have a boolean value to indicate this but how does that information get back to the sender? Thanks in advance, Eric

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      You could just use CancelEventArgs. ;P xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

      1 Reply Last reply
      0
      • B Beringer

        I've been working with my own custom events for controls and thought I had it figured out until I wanted to implement "Before" events(you know like BeforeExpand in TreeView). This kind of event will have a "Cancel" or "Handled" flag to indicate to the sender that the event was canceled (or handled) by the subscriber to the event. My question is, as the sender of the event, how do I know about the Canceling or handling? I understand that the eventargs object will have a boolean value to indicate this but how does that information get back to the sender? Thanks in advance, Eric

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        Beringer wrote: but how does that information get back to the sender Because the event args are sent as a reference, so when control returns to your event handler the boolean value will be retained. assuming the following FooEventArgs have a "CancelFoo" boolean property

        private BeforeFooDelegate beforeFoo
        
        private void onBeforeFoo(FooEventArgs e)
        {
            if(beforeFoo != null)
              beforeFoo(this,e); // listeners can set e.CancelFoo = true to cancel the execution of Foo
         
            if(!e.CancelFoo) // if it is not cancelled - do Foo
              this.Foo();
        }
        
        B 1 Reply Last reply
        0
        • B Beringer

          I've been working with my own custom events for controls and thought I had it figured out until I wanted to implement "Before" events(you know like BeforeExpand in TreeView). This kind of event will have a "Cancel" or "Handled" flag to indicate to the sender that the event was canceled (or handled) by the subscriber to the event. My question is, as the sender of the event, how do I know about the Canceling or handling? I understand that the eventargs object will have a boolean value to indicate this but how does that information get back to the sender? Thanks in advance, Eric

          M Offline
          M Offline
          maheswara
          wrote on last edited by
          #4

          Cancel Supported by the .NET Compact Framework. Gets or sets a value indicating whether the event should be canceled. // Calls this method from the InitializeComponent() method of your form private void OtherInitialize() { this.Closing += new CancelEventHandler(this.Form1_Cancel); this.myDataIsSaved = new Boolean(); this.myDataIsSaved = true; } protected void Form1_Cancel (Object sender, CancelEventArgs e) { if (!myDataIsSaved) { e.Cancel = true; MessageBox.Show("You must save first."); } else { e.Cancel = false; MessageBox.Show("Goodbye."); } } mahes

          B 1 Reply Last reply
          0
          • M maheswara

            Cancel Supported by the .NET Compact Framework. Gets or sets a value indicating whether the event should be canceled. // Calls this method from the InitializeComponent() method of your form private void OtherInitialize() { this.Closing += new CancelEventHandler(this.Form1_Cancel); this.myDataIsSaved = new Boolean(); this.myDataIsSaved = true; } protected void Form1_Cancel (Object sender, CancelEventArgs e) { if (!myDataIsSaved) { e.Cancel = true; MessageBox.Show("You must save first."); } else { e.Cancel = false; MessageBox.Show("Goodbye."); } } mahes

            B Offline
            B Offline
            Beringer
            wrote on last edited by
            #5

            Thank you for your help.

            1 Reply Last reply
            0
            • J J4amieC

              Beringer wrote: but how does that information get back to the sender Because the event args are sent as a reference, so when control returns to your event handler the boolean value will be retained. assuming the following FooEventArgs have a "CancelFoo" boolean property

              private BeforeFooDelegate beforeFoo
              
              private void onBeforeFoo(FooEventArgs e)
              {
                  if(beforeFoo != null)
                    beforeFoo(this,e); // listeners can set e.CancelFoo = true to cancel the execution of Foo
               
                  if(!e.CancelFoo) // if it is not cancelled - do Foo
                    this.Foo();
              }
              
              B Offline
              B Offline
              Beringer
              wrote on last edited by
              #6

              Thank you for your help.

              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