Reputation: 15
I have a form that has a set of labels that can be clicked - See Screenshot (Labels 93 to Label 21 on the Player side and labels 10 to 22 on the computer side).
All I want to do is change the label back colour when the control is clicked. This I have figured out.
My question is more about clean code. Instead of having
private void Playerlabel_Click(object sender, EventArgs e)
{
lblPlayerCardInt.BackColor = Color.FromArgb(219, 255, 248);
}
private void Playerlabel2_Click(object sender, EventArgs e)
{
lblPlayerCardStrength.BackColor = Color.FromArgb(219, 255, 248);
}
//repeat 5 times and then another 5 times for the computer labels.
Is there a way of defining a method so that no matter if I click the label or the computer clicks it, the method of highlighting the label is called.
I'm think something along the lines of
public void HighlightLabel()
{
foreach (Control x in this.Controls)
{
if (x is Label)
{
((Label)x).BackColor = Color.FromArgb(219, 255, 248);
}
}
}
Is this the right approach? It may seem like an obvious question, but new to programming in C#/OOP so want to be sure I'm doing it the correct way.
Upvotes: 0
Views: 131
Reputation: 661
This should do the trick for you and register the same click event for all your labels on the form.
this.Controls
.OfType<Label>()
.AsParallel()
.ForAll(x => x.Click += (o,e) => x.BackColor= Color.FromArgb(219, 255, 248));
Upvotes: 0
Reputation: 5156
Seeing as all you're doing is setting the backcolor each time why not just have 1 method and do something like:
private void label_Click(object sender, EventArgs e)
{
Label mylabel = (Label) sender;
mylabel.backcolor = Color.FromArgb(219, 255, 248);
}
You can then just put Label_Click in as the click event for each label rather than doing a separate method for each label.
However, if you want to do more based on the label you can use the sender to determine the label clicked. Something like this:
private void label_Click(object sender, EventArgs e)
{
Label mylabel = (Label) sender;
//Determine which label has been clicked by name or id for example
switch(myLabel.Name)
{
case "lblPlayerCardint":
//Do something
break;
}
}
Upvotes: 2
Reputation: 3956
Yes you can set only 1 Click event-handler
for all those labels:
Step 1:
private void lbl_Click(object sender, EventArgs e)
{
Label lbl = sender as Label;
lbl.BackColor = Color.FromArgb(219, 255, 248);
}
Step 2:
If you create labels on design time then do the following for each label:
lbl_Click
event-handler from the dropdown listOR
If your create labels programatically then use this instead:
lblPlayerCardInt.Click += new EventHandler(lbl_Click);
lblPlayerCardStrength.Click += new EventHandler(lbl_Click);
...
Upvotes: 2
Reputation: 189
You could cast the event sender object to a label
private void label_Click(object sender, EventArgs e)
{
Label lblClicked=sender as Label;
lblClicked.BackColor = Color.FromArgb(219, 255, 248);
}
Upvotes: 1
Reputation: 460258
You could use OfType
which is part of the System.Linq
namespace:
foreach(Label lbl in this.Controls.OfType<Label>())
lbl.BackColor = Color.FromArgb(219, 255, 248);
Upvotes: 0