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

2

I created a calculator using Windows Forms, it ran perfectly, but when I click the operation button before the number button (to enter a value on the display) an error occurs that closes my program alone. I wanted to know how I can do so that the operation buttons are initially disabled, and after clicking on the number button is available to carry out the operation. Thanks for the help ..

    
asked by anonymous 03.06.2016 / 00:53

2 answers

3

You have two solutions:

1 - Use try / catch , where in case of an error your program will fall into catch and then you will see a message or something like that.

2 - As @Jesters pointed out in Load of your Form all the calculation buttons are disabled (*, +, -,%). It is only after you click on one of the numbers to perform your operation that these buttons will be enabled and available for operation.

No load of your Form:

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

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

btnSoma.Enabled = True;
btnSubt.Enabled = True;
btnMult.Enabled = True;
btnDivid.Enabled = True;
    
03.06.2016 / 13:22
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 click event of the number button you put btnSomar.Enabled = true; to enable the button.

    
03.06.2016 / 13:07