问题
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