简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM