views:

35

answers:

1

I'm going to briefly explain what I want my program to do.

I have a lot of Images on my form and I want the image source to change on MouseEnter event.

So, if a user moves the mouse over the button, I'd like the button to appear to be glowing. Of course I've made two images for the Image control. One normal, and one glowing. I'm trying to make a single event on mouseEnter for all of the images because I don't want to pollute my code with 60+ events all essentially doing the same thing.

Someone suggested I do something like this:

void HeroMouseEnter(object sender, EventArgs e)
{    
    ((PictureBox)sender).Image =  GetImage(((PictureBox)sender).Name)           
}

Honestly, this would work exactly how I need it to. But I'm a bit confused is about the GetImage() method.

How exactly would I code this? All of my images, both the glowing and non glowing ones are already added to my resources. How would I summon them according to the PictureBox's name?

I tried making a dictionary with the key being the name of the pictureBox and the value being the resource file, but no dice.

Please help!

+1  A: 

Something like this?

    public Image GetImage(string name)
    {
        switch (name)
        {
            case "PictureBox1":
                return Properties.Resources.Picture1;
            case "PictureBox2":
                return Properties.Resources.Picture2;
            default:
                return null;
        }
    }
Cory Charlton
If your pictures boxes were named exactly the same as your resource you could use reflection to get them, then you wouldn't need to maintain a switch case statement :)
Paul Creasey