I am trying to handle mouseover and mouseleave events for multiple controls with the same method. There are 10 different buttons in my form that I need to handle with the mouse. Now I usually do something like this:
public Form1() { InitializeComponent(); button1.MouseEnter += new EventHandler(button1_MouseEnter); button1.MouseLeave += new EventHandler(button1_MouseLeave); } void button1_MouseLeave(object sender, EventArgs e) { this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1)); } void button1_MouseEnter(object sender, EventArgs e) { this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2)); }
But since then I have 10 controls that are basically handled the same way, I find it inappropriate to make a separate method for each control, in my case there are 20 methods that will do almost the same thing, but with a different control.
So, there is a way to integrate these events into one and simply determine which control should be processed.
Edit:
Can you determine which exact control raised the event, let's say I have an image that changes the image depending on the hover button.
source share