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. error when i try to not exit with program when i press button no in messagebox

error when i try to not exit with program when i press button no in messagebox

Scheduled Pinned Locked Moved C#
questioncsharphelp
10 Posts 5 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.
  • U Offline
    U Offline
    User3490
    wrote on last edited by
    #1

    hi i have a program in c# that when i press button exit it appears a message box that tell you if you want to exit from the program or not. But if you press No the program make exit. private void btnLogout_Click(object sender, EventArgs e) { if (MessageBox.Show("are you sure?you want to exit?;","", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Application.Exit(); } }

    OriginalGriffO B 2 Replies Last reply
    0
    • U User3490

      hi i have a program in c# that when i press button exit it appears a message box that tell you if you want to exit from the program or not. But if you press No the program make exit. private void btnLogout_Click(object sender, EventArgs e) { if (MessageBox.Show("are you sure?you want to exit?;","", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Application.Exit(); } }

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Not from that code - if I paste it in and hook up the event then it does what I expect. Shows the box, press Yes and it exits. Press No and it doesn't. Check that you are executing the right code: put a breakpoint on the if line, and see if the debugger hits it.

      The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      R U 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        Not from that code - if I paste it in and hook up the event then it does what I expect. Shows the box, press Yes and it exits. Press No and it doesn't. Check that you are executing the right code: put a breakpoint on the if line, and see if the debugger hits it.

        The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

        R Offline
        R Offline
        Ron Nicholson
        wrote on last edited by
        #3

        Sometimes when I have an issue like this, I'll break it out. Store the result of the messagebox in a variable and stop at that point. You can see exactly what is happening each step. What OriginalGriff said is the first step.

        P 1 Reply Last reply
        0
        • R Ron Nicholson

          Sometimes when I have an issue like this, I'll break it out. Store the result of the messagebox in a variable and stop at that point. You can see exactly what is happening each step. What OriginalGriff said is the first step.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          I'm pretty sure that OriginalGriff knew this. You replied to OG, and not to the OP - who won't be notified that there's been a reply to a reply.

          I was brought up to respect my elders. I don't respect many people nowadays.
          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Not from that code - if I paste it in and hook up the event then it does what I expect. Shows the box, press Yes and it exits. Press No and it doesn't. Check that you are executing the right code: put a breakpoint on the if line, and see if the debugger hits it.

            The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

            U Offline
            U Offline
            User3490
            wrote on last edited by
            #5

            the debugger does not hit anywhere and still working with strange way.

            OriginalGriffO 1 Reply Last reply
            0
            • U User3490

              the debugger does not hit anywhere and still working with strange way.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Then you need to start by looking at the button you press in the designer, and see where a click event is taking you. Because if the debugger isn't hitting the if condition, then it isn't executing that method...:laugh:

              The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • U User3490

                hi i have a program in c# that when i press button exit it appears a message box that tell you if you want to exit from the program or not. But if you press No the program make exit. private void btnLogout_Click(object sender, EventArgs e) { if (MessageBox.Show("are you sure?you want to exit?;","", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Application.Exit(); } }

                B Offline
                B Offline
                Bernhard Hiller
                wrote on last edited by
                #7

                Is that btnLogout also the CancelButton of your form?

                U 1 Reply Last reply
                0
                • B Bernhard Hiller

                  Is that btnLogout also the CancelButton of your form?

                  U Offline
                  U Offline
                  User3490
                  wrote on last edited by
                  #8

                  yes i had set it as cancel button simultaneously.

                  B 1 Reply Last reply
                  0
                  • U User3490

                    yes i had set it as cancel button simultaneously.

                    B Offline
                    B Offline
                    Bernhard Hiller
                    wrote on last edited by
                    #9

                    Then you must handle the Form Closing event, and set its Cancel property to true in case you do not want to close the form (in btnLogout_Click just do a this.Close()).

                    U 1 Reply Last reply
                    0
                    • B Bernhard Hiller

                      Then you must handle the Form Closing event, and set its Cancel property to true in case you do not want to close the form (in btnLogout_Click just do a this.Close()).

                      U Offline
                      U Offline
                      User3490
                      wrote on last edited by
                      #10

                      for the use that i want it i just make none the cancel button property that has the logout_click button :) so i use only the command application.exit(); and when i press button logout and in messagebox NO then it stay in the form that i am at the same time

                      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