VB.Net If statement to check if a specific image from my resources is loaded

我的未来我决定 提交于 2019-12-12 06:18:40

问题


I want to use an If statment to check if a specific image from my resources is loaded.

I want to change the image when it is clicked as in:

If PictureBox1.Image = My.Resources.BIKE13 Then
    PictureBox1.Image = My.Resources.BIKE13_Helmet
End If

I'm going to use several ElseIf Statements


After some search I found several ways to do it but I have about 20-30 images and I'm and I need a simple method.

(VS 2010)


回答1:


That code cannot possibly work for two reasons. Firstly, an Image is a reference type object, so to compare like that you would have to use the Is operator rather than =. Secondly, My.Resources creates a new object each time, so even using Is will never give you a match.

What you need to do is use the My.Resources property once and once only and assign the Image object to a member variable. You can then use Is with that field.

E.g.

Private bike13Image As Image = My.Resources.BIKE13
Private bike13HelmetImage As Image = My.Resources.BIKE13_Helmet

and

If PictureBox1.Image Is bike13Image Then
    PictureBox1.Image = bike13HelmetImage
End If


来源:https://stackoverflow.com/questions/32811840/vb-net-if-statement-to-check-if-a-specific-image-from-my-resources-is-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!