Jay Chuah
Jay Chuah

Reputation: 124

How to compare two image uisng vb.net?

I got the image in the resources in my project.

I assign the image to the button by using
btnViewProductRemove.Image = My.Resources.btnRemove

Then I wish to compare the image because if the image is My.Resources.btnRemove then I wish to perform remove action, else I wish to perform recover action.
Here is my coding:

If btnViewProductRemove.Image.Equals(My.Resources.btnRemove) Then
        'Perform Remove
    Else
        'Perform Recover
End If

However, this code does not working. Maybe the bitmap is different? But there are same image. How do I can compare two image?

Upvotes: 0

Views: 970

Answers (1)

Ry-
Ry-

Reputation: 225164

My.Resources.btnRemove is a property that returns a new instance of an image every time. You could make it work by holding onto a reference of your resource images, but it’s kind of inappropriate to decide what to do based on a button’s image. The Tag property would be marginally more appropriate: set btnViewProductRemove.Tag = "remove" or = "recover" at the same time as you set its image, then just compare that.

If CStr(btnViewProductRemove.Tag) = "Remove" Then
    ' Remove
Else
    ' Recover
End If

If you can, though, I’d try to make the Remove and Recover buttons two separate controls, with only one of them visible at a time.

Upvotes: 2

Related Questions