3

I want to check if a button is clicked with c#

like this

private void btnFillo_Click(object sender, EventArgs e)
{
    btnFillo.Text = "text";

  //  if (btnFillo clicked again) {
        // do something
  //  }
}

3 Answers3

3
      private int  clickCounter = 0;
      private void btnFillo_Click (object sender, EventArgs e) {
         btnFillo.Text = "text";

         if (clickCounter >= 1) {
         // do something
         clickCounter = 0;
         }
         else  clickCounter += 1;
      }

if you want to do something just on second clicks simply use a boolean:

      private bool  isClicked = false;
      private void btnFillo_Click (object sender, EventArgs e) {
         btnFillo.Text = "text";

         if (isClicked) {
         // do something
            isClicked = false;
         } else isClicked = true;
      }
AliReza Sabouri
  • 4,355
  • 2
  • 25
  • 37
  • Thanks for code, Can you explain the logic how it go ? please – Gassalam Frans Feb 07 '20 at 16:48
  • the logic is very simple but: the global `isClicked` is always false until the user clicks on the button. when user clicked for the first time we change value of isClicked to true. so next time your if condition works ... at the end of your if condition when second click did its job we reseting isClicked state to false again... – AliReza Sabouri Feb 07 '20 at 16:53
  • 1
    @GassalamFrans You're keeping a boolean (true or false) variable. When it is clicked the first time, `isClicked` is false so the `if (isClicked)` expression will be false and the statement it sets up wont run. Then you set `isClicked` to `true`. Now the second time a user clicks the button, the `if (isClicked)` expression will be true and the statement will run. – TheBatman Feb 07 '20 at 16:53
  • @AliReza, TheBatman thanks both for this good explanation. Thank you very much. – Gassalam Frans Feb 07 '20 at 17:11
  • 1
    @AliReza Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score. – Gassalam Frans Feb 07 '20 at 17:42
0

You have to save click in a global variable, such as

clicked += 1

where clicked variable is a global variable (var clicked = 0). And after:

if(clicked > 1)

or use Control.MouseDoubleClick Event: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.mousedoubleclick?redirectedfrom=MSDN&view=netframework-4.8

Péter
  • 273
  • 1
  • 10
-1

thanks all for the help i solve my problem... I post the code for other if they need this.

private int clickCounter = 0;
        private void btnFillo_Click(object sender, EventArgs e)
        {

            if(clickCounter == 0 ) {
                // first time click
                btnFillo.Text = "text";
                clickCounter++;
            }
            else if (clickCounter == 1)
            {
                // second time click
                btnFillo.Text = "heeeeellll";
                clickCounter++;
            }
            else if (clickCounter == 2)
            {
                // third time click
                btnFillo.Text = "change 2";
                clickCounter = 0;
            }
            else // you can do more if you want more clicks
            {
                clickCounter += 1;

            }
        }