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. bypassing the button event

bypassing the button event

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

    hi friends, how can i bypass the button event on certain conditions?:confused: like i can in while() loop using break statement.

    C 1 Reply Last reply
    0
    • M maheshfour

      hi friends, how can i bypass the button event on certain conditions?:confused: like i can in while() loop using break statement.

      C Offline
      C Offline
      Christian Wikander
      wrote on last edited by
      #2

      You can attach and detach the eventhandler. If you look at the "Windows Form Designer generated code" you can see how the connection between the event and the code is done. If the code that should be triggered by the event is in button1_Click the code would be as follows: private void AttachEventHandler() { this.button1.Click += new System.EventHandler(button1_Click); } private void DetachEventHandler() { this.button1.Click -= new System.EventHandler(button1_Click); } But why don't you just check the conditions using an if statement in the event code?

      A 1 Reply Last reply
      0
      • C Christian Wikander

        You can attach and detach the eventhandler. If you look at the "Windows Form Designer generated code" you can see how the connection between the event and the code is done. If the code that should be triggered by the event is in button1_Click the code would be as follows: private void AttachEventHandler() { this.button1.Click += new System.EventHandler(button1_Click); } private void DetachEventHandler() { this.button1.Click -= new System.EventHandler(button1_Click); } But why don't you just check the conditions using an if statement in the event code?

        A Offline
        A Offline
        ameto
        wrote on last edited by
        #3

        thanks for replying i tried using this.button1.Click -= new System.EventHandler(button1_Click); inside the button_click event but the next time i click the button nothing happens.

        C 1 Reply Last reply
        0
        • A ameto

          thanks for replying i tried using this.button1.Click -= new System.EventHandler(button1_Click); inside the button_click event but the next time i click the button nothing happens.

          C Offline
          C Offline
          Christian Wikander
          wrote on last edited by
          #4

          Ofcourse. My example detach the link between the event and the code. You will have to use the attach function to restore the link. In .NET, the Event - Code relationship is not like in VB6. You can programmaticly set up what code to execute when an event is triggered. You can even add more than one function to be called. I assumed this was what you were trying to do. If this is not what you need, I think you should add an if statement in the button1_Click function that checks the conditions before executing the code.

          M 1 Reply Last reply
          0
          • C Christian Wikander

            Ofcourse. My example detach the link between the event and the code. You will have to use the attach function to restore the link. In .NET, the Event - Code relationship is not like in VB6. You can programmaticly set up what code to execute when an event is triggered. You can even add more than one function to be called. I assumed this was what you were trying to do. If this is not what you need, I think you should add an if statement in the button1_Click function that checks the conditions before executing the code.

            M Offline
            M Offline
            maheshfour
            wrote on last edited by
            #5

            actually i have a main form which calls the input form as modal. there is OK and cancel button on the input form. if the user presses cancel the form closes. if the user presses OK the software checks if all the inputs are correct. in the main form i am using the dialogresult property of the input form. i have set ok as dialogresult of the OK button. so irrespective ofthe validity of the inputs the input form returns the dislog result. this is why i was trying to exit the buttonOk_click event if the inputs were not valid.

            M P 2 Replies Last reply
            0
            • M maheshfour

              actually i have a main form which calls the input form as modal. there is OK and cancel button on the input form. if the user presses cancel the form closes. if the user presses OK the software checks if all the inputs are correct. in the main form i am using the dialogresult property of the input form. i have set ok as dialogresult of the OK button. so irrespective ofthe validity of the inputs the input form returns the dislog result. this is why i was trying to exit the buttonOk_click event if the inputs were not valid.

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

              This can be solved very easily, without having to fiddle with button event handlers. In your button event handler you could do something like this:

              private void OKButton_Click(object sender, EventArgs e)
              {
              bool inputsOk = true;
              // ... check your inputs and set inputsOk to false if one input
              // is invalid
              if ( !inputsOk )
              this.DialogResult = DialogResult.None;
              else
              {
              this.DialogResult = DialogResult.Ok;
              this.Close();
              }
              }

              That way the Dialog will stay open unless all inputs are valid. mav

              M 1 Reply Last reply
              0
              • M mav northwind

                This can be solved very easily, without having to fiddle with button event handlers. In your button event handler you could do something like this:

                private void OKButton_Click(object sender, EventArgs e)
                {
                bool inputsOk = true;
                // ... check your inputs and set inputsOk to false if one input
                // is invalid
                if ( !inputsOk )
                this.DialogResult = DialogResult.None;
                else
                {
                this.DialogResult = DialogResult.Ok;
                this.Close();
                }
                }

                That way the Dialog will stay open unless all inputs are valid. mav

                M Offline
                M Offline
                maheshfour
                wrote on last edited by
                #7

                i had tried te same thing but it does not work. in the ok button property i have set the dialog result property as none. i have to press the OK button twice to assign the dialog result property to the button

                M 1 Reply Last reply
                0
                • M maheshfour

                  i had tried te same thing but it does not work. in the ok button property i have set the dialog result property as none. i have to press the OK button twice to assign the dialog result property to the button

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

                  Then you must have some other errors in your code. The DialogResult property of your Button just determines the value of your Form's DialogResult property when the button is pressed. At the end of your button event handler, the dialog is closed if the Form's DialogResult property is not DialogResult.None, simple as that. If you don't want the dialog to close you have to set it's DialogResult to DialogResult.None. So in my example, the call to this.Close(); is superfluous, but I wanted to show you where the dialog gets closed. mav

                  P 1 Reply Last reply
                  0
                  • M maheshfour

                    actually i have a main form which calls the input form as modal. there is OK and cancel button on the input form. if the user presses cancel the form closes. if the user presses OK the software checks if all the inputs are correct. in the main form i am using the dialogresult property of the input form. i have set ok as dialogresult of the OK button. so irrespective ofthe validity of the inputs the input form returns the dislog result. this is why i was trying to exit the buttonOk_click event if the inputs were not valid.

                    P Offline
                    P Offline
                    Pushkar Pathak
                    wrote on last edited by
                    #9

                    U can overcome this by many ways firstly make checks in the main form after the dialog result returned OK and if the validity check fails showdialog the input form again otherwise to keep the main form free from the validity check mechanism check in the button click event ... since a modal dialog is not destroyed until disposed raise a flag that says "Wrong INput" and let main form check this flag and show the main form again ..... or set the dialog result for the form and the button to none and set the dialogresult for the form in the event if the check is alright so that clicking the button doesnt close the form but u do programitically when reqd and that the main for sees only the dialog result for the form .. take a look at this http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskretrievingdialoginformation.asp[^] Pushkar Pathak

                    1 Reply Last reply
                    0
                    • M mav northwind

                      Then you must have some other errors in your code. The DialogResult property of your Button just determines the value of your Form's DialogResult property when the button is pressed. At the end of your button event handler, the dialog is closed if the Form's DialogResult property is not DialogResult.None, simple as that. If you don't want the dialog to close you have to set it's DialogResult to DialogResult.None. So in my example, the call to this.Close(); is superfluous, but I wanted to show you where the dialog gets closed. mav

                      P Offline
                      P Offline
                      Pushkar Pathak
                      wrote on last edited by
                      #10

                      Sorry my bad ... I think mav.northwind has provided the ideal example although there are ways to do it this example is ideal ... i didnt read it before i wrote the reply Pushkar Pathak

                      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