Getting image index from image list

2.6k views Asked by At

I had a quick question. During run time I have populated a imagelist with particular tags. Now I need to obtain the image index of a particular image based on the tag. I'm not too sure how to go about it.

For example:

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

Please note that the function above is not valid but I would want to get the image index if i do something like GetIndex("Documents") with "Documents" as a tag of imagelistitem. Could anyone tell me how I would be able to acheive that. I hope that makes sense.

Update:

    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

The problem now is that the tag is not being indentified and it the function keeps returning -1

1

There are 1 answers

0
hdkhardik On

Try checking your condition by following

If imgList.Images(i).Tag = tag Then

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

End If