从图像列表

时间:2015-06-16 09:04:21

标签: .net vb.net winforms imagelist

我有一个简短的问题。在运行期间,我已经填充了具有特定标签的图像列表。现在我需要根据标签获取特定图像的图像索引。我不太清楚如何去做。

例如:

Dim ImgIndx as integer
Private Function GetIndex(ByVal Tag as string)
If imagelist.item.tag = Tag Then
ImgIndx = imagelist.item.ImageIndex
return ImgIngx

请注意,上面的函数无效但我想获取图像索引,如果我使用" Documents"以及#34;文件"来执行GetIndex(" Documents")之类的操作。作为imagelistitem的标签。谁能告诉我如何能够实现这一目标。我希望这是有道理的。

更新

    Private Function TagToIndex(imgList As ImageList, tag As String) As Integer

    ' Start a for loop where i would be equal to the image index
    For i As Integer = 0 To imgList.Images.Count - 1

        ' If the index you're checking has a tag equal to the one you're looking for then...
        If imgList.Images(i).Tag Is tag Then

            ' Return the index of that tag
            ' Function will exit upon returning a value
            Return i

        End If

    Next
     Return -1

End Function

现在的问题是标签没有被识别,而且函数一直在返回-1

1 个答案:

答案 0 :(得分:0)

按照

尝试检查您的情况
If imgList.Images(i).Tag = tag Then

            ' Return the index of that tag
            ' Function will exit upon returning a value
            Return i

End If