Selected Checkbox...
-
Hi. i have a few checkboxes on my form and i want to show a simple messagebox when one of those checkboxes are checked. how can i do this dynamically? for instance, instead of writing an if statement per checkbox, like this:
if(checkbox1.checked = true) { MessageBox.Show("checked"); }
write it dynamically, like this: (the is the name of the selected checkbox)if(.checked = true) { MessageBox.Show("checked"); }
i know this is pretty easy for all you guru friends out there. :) Thanks, .gonad. -
Hi. i have a few checkboxes on my form and i want to show a simple messagebox when one of those checkboxes are checked. how can i do this dynamically? for instance, instead of writing an if statement per checkbox, like this:
if(checkbox1.checked = true) { MessageBox.Show("checked"); }
write it dynamically, like this: (the is the name of the selected checkbox)if(.checked = true) { MessageBox.Show("checked"); }
i know this is pretty easy for all you guru friends out there. :) Thanks, .gonad.I'm not sure if I understand you, but you can maybe use first parameter of your event handler method (sender). You have to set same function as event handler for all checkboxes.
private void checkBox1\_CheckedChanged(object sender, System.EventArgs e) { CheckBox checked=(CheckBox)sender; // checked is checkbox that was just checked/unchecked }
i'm only pointer to myself
-
I'm not sure if I understand you, but you can maybe use first parameter of your event handler method (sender). You have to set same function as event handler for all checkboxes.
private void checkBox1\_CheckedChanged(object sender, System.EventArgs e) { CheckBox checked=(CheckBox)sender; // checked is checkbox that was just checked/unchecked }
i'm only pointer to myself
Hi. Thanks for your help. this worked exactly as you said, but i do have a queston. Can you explain what the
(CheckBox)sender
is specifying? also, can i use this for a textbox, or other controls, as well? for example,TextBox tb = (TextBox)sender;
I'm new at this stuff, can't you tell? Thanks again. .gonad. -
Hi. Thanks for your help. this worked exactly as you said, but i do have a queston. Can you explain what the
(CheckBox)sender
is specifying? also, can i use this for a textbox, or other controls, as well? for example,TextBox tb = (TextBox)sender;
I'm new at this stuff, can't you tell? Thanks again. .gonad.First parameter (
sender
) of event handler is affected object. it is passed as object so you need to cast it to correct type (Button / CheckBox etc..). So when you have event handler for Button,sender
is button user clicked on. If you have event handler for TextBox,sender
is this TextBox. i'm only pointer to myself