How to disable a button and to activate it, click on another button?

Asked

Viewed 4,495 times

2

I created a calculator using Windows Forms, it ran perfectly, however when clicking the operation button before the number button (to insert a value in the display) an error happens that closes my program alone.
I wanted to know how I can do so that initially the operation Buttons are disabled, and after clicking the number button is available to carry out the operation. I appreciate the help..

  • Within the code that the button number runs, you can put a btnCalcular.Enabled = true;. Since you need to leave the value of Enabled his false.

2 answers

3


You have two solutions:

1 - Using the Try / catch , where if an error occurs your program will fall into the catch and then you show a message or something like.

2 - As @Jeterson said in Load of your Form all calculation buttons are disabled ( * , + , - , % ). Only after you click on one of the numbers to perform their operation will these buttons be activated and available to be operated.

No load of your Form:

btnSoma.Enabled = False;
btnSubt.Enabled = False;
btnMult.Enabled = False;
btnDivid.Enabled = False;

Finally, in the event Click of the numbers, when one of them is clicked you will activate the Textbox of operations:

btnSoma.Enabled = True;
btnSubt.Enabled = True;
btnMult.Enabled = True;
btnDivid.Enabled = True;

2

In the constructor of your form class you put the button to start disabled.

public Calculadora(){
InicializaComponent();
btnSomar.Enabled = false;
//outros botoes que voce queira deixar desabilitados
}

Now in the event click the number button you place btnSomar.Enabled = true; to enable the button.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.