简体   繁体   English

带有if语句的picutrebox

[英]picutrebox with if statement

I am using VS2008 with VB.NET Compact Framework 3.5 to develop a project. 我正在使用VS2008与VB.NET Compact Framework 3.5来开发一个项目。 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? Imagelist中有3个图像,索引为0,1,2。有没有办法用if语句编写代码,如下所示?

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. 我也尝试使用Is而不是“=”,如下所示,但仍然无法正常工作。 In debug, the statement return false, so it's never run 'do something. 在debug中,语句返回false,因此它永远不会运行'做某事。

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: 更新图片框时,将当前索引存储在.Tag属性中,以便对其进行评估:

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. 注意:case语句类似于If语句,键入次数少,可读性更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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