2013-10-22 106 views
0

我正在使用VS2008与VB.NET Compact Framework 3.5开发一个项目。我有一个从图像列表加载图片的图片框。在Imagelist中有3张图像,索引为0,1,2。有没有什么办法可以用来编写代码,如果的语句如下所示?picutrebox与if语句

加载窗体时:

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 

我也试过用的是代替“=”,如下所示,但仍然无法工作。在调试中,该语句返回false,所以它从不运行'执行某些操作。

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

在此先感谢。

回答

2

当您更新PictureBox的,存储在.TAG属性的当前索引,所以你可以评价它:

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

后来:

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 

注:case语句类似于如果语句的键入少且易读性强。

+0

只是想知道为什么我的原始代码不起作用。即使这返回False: MsgBox(ImageList1.Images(1)is ImageList1.Images(1)) – apolloneo