Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# replacing integer

Tags:

c#

I have 50 buttons in my project as all are linked, when pressed, to a method. And now, when a button has been pressed I want it to go invisible. Since I don't want my code to contain 50 IF statements to check which button that has been pressed:

If(sender == Button1)
{     
    Button1.visible = false;
} 

This code gets very long if I ill have almost the same block of code when only the button name changes 50 times. Is there anyway to this in another way to get a shorter code?

Maybe: If a String variable contains the name of the button?

string buttoncheck = Button1;

And then in the upper code insert buttoncheck instead of Button1 since buttoncheck contains the value/name of Button1?

Thanks!

like image 443
laz Avatar asked Jul 19 '26 09:07

laz


1 Answers

Try something like

var x = sender as Button;
if(x != null){
    x.Visible = false;
}
like image 128
YetAnotherUser Avatar answered Jul 20 '26 21:07

YetAnotherUser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!