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 event

cancel event

Scheduled Pinned Locked Moved C#
4 Posts 2 Posters 1 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.
  • D Offline
    D Offline
    Dominic Farr
    wrote on last edited by
    #1

    I have a windows form with a button that I have set the DialogResult property. The button also has a click event. When the click event fires, my event methods displays an MessageBox with Yes or No buttons. (an 'are you sure' type message) If the user clicks the No button I want to cancel closing of the window form event. How I can I achieve this. Thanks

    D 1 Reply Last reply
    0
    • D Dominic Farr

      I have a windows form with a button that I have set the DialogResult property. The button also has a click event. When the click event fires, my event methods displays an MessageBox with Yes or No buttons. (an 'are you sure' type message) If the user clicks the No button I want to cancel closing of the window form event. How I can I achieve this. Thanks

      D Offline
      D Offline
      DigitalKing
      wrote on last edited by
      #2

      private void button1_Click(object sender, EventArgs e)
      {
      DialogResult dr = MessageBox.Show("Are you sure you want to close this window?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
      if (dr == DialogResult.Yes)
      {
      this.Close();
      }
      //if anything other than Yes is clicked (like No), then nothing will happen.
      }

      D 1 Reply Last reply
      0
      • D DigitalKing

        private void button1_Click(object sender, EventArgs e)
        {
        DialogResult dr = MessageBox.Show("Are you sure you want to close this window?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (dr == DialogResult.Yes)
        {
        this.Close();
        }
        //if anything other than Yes is clicked (like No), then nothing will happen.
        }

        D Offline
        D Offline
        Dominic Farr
        wrote on last edited by
        #3

        The problem stems from button1 having it's DialogResult property set. When button1 click event is fired, it executes button1_Click, displays the are you sure dialog and regardless of the result of the are you sure dialog will close the window form because it has a DialogResult set. I was wondering if I could cancel this close window event depending on the result of the are you sure dialog. I can simply not set the DialogResult property of button1, and set the DialogResult in the button1_Click event depending on the result of the are you sure dialog. Regards.

        D 1 Reply Last reply
        0
        • D Dominic Farr

          The problem stems from button1 having it's DialogResult property set. When button1 click event is fired, it executes button1_Click, displays the are you sure dialog and regardless of the result of the are you sure dialog will close the window form because it has a DialogResult set. I was wondering if I could cancel this close window event depending on the result of the are you sure dialog. I can simply not set the DialogResult property of button1, and set the DialogResult in the button1_Click event depending on the result of the are you sure dialog. Regards.

          D Offline
          D Offline
          DigitalKing
          wrote on last edited by
          #4

          Why not remove the DialogResult from button1 and assign it to the window only if it is closing:

          private void button1_Click(object sender, EventArgs e)
          {
          DialogResult dr = MessageBox.Show("Are you sure you want to close this window?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
          if (dr == DialogResult.Yes)
          {
          this.DialogResult = DialogResult.Cancel; //or whatever
          this.Close();
          }
          //if anything other than Yes is clicked (like No), then nothing will happen.
          }

          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