Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling a button in C# WITHOUT setting btn.Enabled = false;?

Is there any other way beside the button1.Enabled = false; to disable the user from clicking it?

I'm implementing simple TicTacToe game in WinForms and if I use the Enabled property the colors of the button's text is turning into gray which is ruining my user interface a lot.

I would like to keep my formatting for the button and just disable further clicking from the user.

like image 650
Don Exo Avatar asked May 11 '13 19:05

Don Exo


People also ask

What is the use of button in C?

Button in C#. A Button is an essential part of an application, or software, or webpage. It allows the user to interact with the application or software. For example, if a user wants to exit from the current application so, he/she click the exit button which closes the application.

How do I add a button to a form in C?

Button in C# - GeeksforGeeks. Step 1: Create a button using the Button () constructor is provided by the Button class. Step 2: After creating Button, set the properties of the Button provided by the Button class. Step 3: And last add this button control to form using Add () method.

How to create your own button using the button class?

2. Run-Time: It is a little bit trickier than the above method. In this method, you can create your own Button using the Button class. Step 1: Create a button using the Button () constructor is provided by the Button class. Step 2: After creating Button, set the properties of the Button provided by the Button class.

How to place a button on the windows form?

Use the below steps: Step 2: Drag the Button control from the ToolBox and drop it on the windows form. You are allowed to place a Button control anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the Button control to set the properties of the Button.


1 Answers

The probably simplest way is to detach the event handler once it is called.

void HandleClick(object sender, EventArgs e)
{
    ((Button)sender).Click -= HandleClick;

    // Handle the click  
}
like image 136
Florian Greinacher Avatar answered Sep 25 '22 03:09

Florian Greinacher