开发者

How can I access my applications images that are in my resources?

开发者 https://www.devze.com 2022-12-14 04:12 出处:网络
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.

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 tryin开发者_如何学编程g 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!


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;
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消