i have a programm to write. It's too long to explain, so ill go with an easy example:
there are 9 labels in the C# Form. What i need is a universal piece of code that will allow me to change text of a label depending on which label was clicked. f.e. clicked label number1 so the text of 9th label with "9".
if it is somewhere here, then sorry for not searching, but i don't even know how to name it properly :)
Loading
AlanPosted Apr 15, 2008, 9:59 AM
Suppose your labels have names of label1, label2, ...., label9 and that all their Click events have been wired up to one eventhandler, labelX_Click(). Then you can do what you want with the following code:
private
void labelX_Click(object sender, EventArgs e){
Label lbl = (Label)sender;lbl.Text = lbl.Name.Substring(5);
}
More generally you can get the number of the label using:
int labelNum = int.Parse(lbl.Name.Substring(5));
and then do a 'switch' on labelNum to perform different actions depending on which label was clicked.