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. disable a button

disable a button

Scheduled Pinned Locked Moved C#
questiontutorial
5 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.
  • S Offline
    S Offline
    Sunshine Always
    wrote on last edited by
    #1

    private void button1_Click(object sender, EventArgs e) { button2.Enabled = false; Thread.Sleep(2000); button2.Enabled = true; } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("Button 2"); } in the above code, when i click the button2 while the sleep i on, after the sleep is over, i get the message box in the button2 click event. what is the function of setting enable = false then? how to prevent this? thanks

    C L 2 Replies Last reply
    0
    • S Sunshine Always

      private void button1_Click(object sender, EventArgs e) { button2.Enabled = false; Thread.Sleep(2000); button2.Enabled = true; } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("Button 2"); } in the above code, when i click the button2 while the sleep i on, after the sleep is over, i get the message box in the button2 click event. what is the function of setting enable = false then? how to prevent this? thanks

      C Offline
      C Offline
      Chetan Patel
      wrote on last edited by
      #2

      You are putting the current Thread in sleep stage so, Your button2 is disabled but when you click on it, The event is not fired. because the thread is sleep and it will not accept the click event it actually Fire the event when the thread is in running mode again so at that time the button2 is enabled. if you are still confused then reply this message

      Best Regards, Chetan Patel

      S 1 Reply Last reply
      0
      • C Chetan Patel

        You are putting the current Thread in sleep stage so, Your button2 is disabled but when you click on it, The event is not fired. because the thread is sleep and it will not accept the click event it actually Fire the event when the thread is in running mode again so at that time the button2 is enabled. if you are still confused then reply this message

        Best Regards, Chetan Patel

        S Offline
        S Offline
        Sunshine Always
        wrote on last edited by
        #3

        i dont want to fire the button2 click even when it is disabled. how do i do it?

        M 1 Reply Last reply
        0
        • S Sunshine Always

          i dont want to fire the button2 click even when it is disabled. how do i do it?

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Hello, You have to set the "Enabled" property before the user clicks the button. If you want to make a validation on the/an event itselfe, I would suggest inherit your own Button from System.Windows.Forms.Button. Use a validation method which uses Flags or some other stuff. Override OnMouseDown, which checks the validation method and calls the base method or not! Not calling the base method of OnMouseDown, will prevent the click event from fireing! See this code example: public class SpecialButton : System.Windows.Forms.Button { public SpecialButton() { } private bool flag1 = true; public bool Flag1 { get { return flag1; } set { if(value!=flag1) flag1 = value; } } private bool flag2 = true; public bool Flag2 { get { return flag2; } set { if(value!=flag2) flag2 = value; } } private bool ValidateClick() { //If Flag1 and Flag2 are true, the Click event will be fired return Flag1&&Flag2; } protected override void OnMouseDown(MouseEventArgs e) { if(ValidateClick()) { //Validation ok (Click will fire) base.OnMouseDown (e); } else { //give some feedback to the user (MessageBox) } } }Hope it helps!

          All the best, Martin

          1 Reply Last reply
          0
          • S Sunshine Always

            private void button1_Click(object sender, EventArgs e) { button2.Enabled = false; Thread.Sleep(2000); button2.Enabled = true; } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("Button 2"); } in the above code, when i click the button2 while the sleep i on, after the sleep is over, i get the message box in the button2 click event. what is the function of setting enable = false then? how to prevent this? thanks

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, the way you do it, button2 is not disabled at all, since the 2-second period it is disabled you also "stop the world" by holding off the GUI thread. don't put Thread.Sleep() in an event handler, it stops the GUI, which you never want to happen. Instead, disable button2 and organize something to re-enable the button later. For instance launch a Windows.Forms.Timer; when that timer expires, re-enable button2. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


            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