Reputation:
If PictureBox1.Image = "a.png" Then
PictureBox1.Image = My.Resources.b
Else
PictureBox1.Image = My.Resources.a
End If
It won't work. How do i make this thing working? It shoud check if the picture box is showing the picture a , if yes then make it showing picture b.
Public Class Form1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If PictureBox1.Image Is My.Resources.b Then
PictureBox1.Image = My.Resources.a
PictureBox1.Refresh()
Else
PictureBox1.Image = My.Resources.b
PictureBox1.Refresh()
End If
End Sub
End Class
Thats the complete code
Upvotes: 1
Views: 1340
Reputation: 27342
Image
is a Property of type System.Drawing.Image whereas "a.png"
is a string so you can't compare these things to see if they are equal.
Also Image
is a reference type, so you must use Is
to compare it to another reference type.
The following might work:
If PictureBox1.Image Is My.Resources.a Then
PictureBox1.Image = My.Resources.b
Else
PictureBox1.Image = My.Resources.a
End If
Note: Comparing images can be tricky because when you set an Image property it may actually create a copy of the original image, so then comparison later does not work. See How to compare Image objects with C# .NET?
So taking that into account, it would be a better idea to use a variable to compare the state as this consumes less resources and is far simpler than having to compare images.
Something like the following should work for you:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Static showImageA As Boolean
showImageA = Not showImageA
If showImageA Then
PictureBox1.Image = My.Resources.a
Else
PictureBox1.Image = My.Resources.b
End If
End Sub
Note: make sure you have Option Strict and Option Explicit On because the code you posted does not compile when you do this and so points out the errors to you.
Upvotes: 2