I was looking to simplify the following action: Click on a panel, and the panel changes its color to green, and if it is green, change it back to gray. I was wondering how you would write the controls into the method as an argument so that it would work for any control without having to duplicate the event method.
Normally: Panel Click event
panel_Click ()
//some if statement
panel1.BackColor = green
panel1.BackColor = gray
then repeat per control. Instead of this, I would create a central method that all of the controls are subscribed to, that read the name of the panel like:
genericpanel_Click(){
ChangeColor(thisPanelname);
}
and then that would make use of the argument/parameter:
public void ChangeColor(panel? Mypanel) {
//some if...
Mypanel.BackColor = Green
Mypanel.BackColor = Gray
}
What's the accurate equivalent of this pseudo code into a working one?
Edit: Okay, yes, I can see now that it's a duplicate, no need to downvote further. I just didn't know what to search for specifically. Anyways, I've found the answer at this point.
object senderparameter in the event method, cast it to panel and then change its colors?