apolloneo
apolloneo

Reputation: 179

picutrebox with if statement

I am using VS2008 with VB.NET Compact Framework 3.5 to develop a project. I have a picturebox which loads pictures from a imagelist. There are 3 images in the Imagelist, with index 0, 1, 2. Is there any way to write a code with an if statement like the following?

When the form loads:

picturebox.image =  imagelist1.Images(0) 'give picture box an initial value

...

If picturebox.image = imagelist1.Images(0) then
    'do something
elseif picturebox.image = imagelist1.Images(1) then
    'do something
elseif picturebox.image = imagelist1.Images(2) then
    'do something
End If

I also tried use Is instead of "=", as follows, but still won't work. In debug, the statement return false, so it's never run 'do something.

If picturebox.image Is imagelist1.Images(0) then
    'do something
End If

Thanks in advance.

Upvotes: 0

Views: 1629

Answers (1)

When you update the picturebox, store the current index in the .Tag property so you can evaluate it:

picturebox.image =  imagelist1.Images(0) 
picturebox.Tag = 0

Later:

Select Case picturebox.Tag
    case 0             ' same as If picturebox.Tag = 0 then
      'do something
    Case 1
      'do something 1
    Case 2
      'do something 2
End Select

Note: A case statement is similar to the If statement with a lot less typing and more readability.

Upvotes: 2

Related Questions