Change Button.BackgroundImage OnClick

AzzKickah

Member
Joined
Mar 19, 2009
Messages
14
Programming Experience
Beginner
Hey people!

I'm from Holland (The Netherlands) and new to this forum, I'm not 100% sure if I'm posting this thread in the correct subforum... plz forgive me if I don't :p
I have a little experience in Visual Basic .NET 2008

Currently I'm developing a program for my job in which you can follow a checklist for doing a certain procedure. With every step you have to click a checkbox and so on. It's saved in an XLS-file and blahblah, all irrelevant for my question.

I've got to the point that I'm almost pulling my hair here, because everything went fine so far with coding my program, but this simple thing I can't solve :mad:

What I'm trying to do is, change the BackgroundImage of a Button when you click on it. If it is Image1 at the time of clicking it should change to Image2, and vice versa.

My code:
VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim CrossColor As Image
        Dim CrossBW As Image
        CrossColor = CheckList.My.Resources.Resources.cross_big
        CrossBW = CheckList.My.Resources.Resources.cross_big_bw1
        If Button2.BackgroundImage = CrossColor Then Button2.BackgroundImage = CrossBW
    End Sub

What am I doing wrong? I googled, I searched this forum, I can't get it to work.
What I think is strange, is that when I just say Button2.BackgroundImage = CrossBW (without the IF - THEN part) it works! :eek:

I'm sure one of you here knows the simple solution to this! :)

Thanks in advance!!
 
Last edited:
I'm unable to view the picture you included in the post but I dont know how the property would be able to compare one image from any other in an IF statement... Does the button have a text property set? You might be better off comparing a value put into the Text or Tag property of the button.

VB.NET:
If Button2.Tag = "CrossBW" Then
   Button2.BackGroundImage = NewImage
   Button2.Tag = "NewImage"
Else 
   Button2.BackgroundImage = CrossBw
   Button2.Tag = "CrossBW"
End If
 
VB.NET:
Dim x As Boolean = False
If x = False then
Button2.backgroundImage = whateverhere
x = true
elseif x = true then
Button2.backgroundImage = whateverhere
x = false
end if
try that, like Tom's code, but I use boolean checks. This is very simple, but untested.

OR

change the = to 'Is', so it would read:
VB.NET:
If Button2.BackgroundImage Is CrossColor
 
I changed the picture in my first post to text (strange though, image is working fine here at my workplace).

And I tried the first suggestion of formlesstree4 but then everytime I click the button, the X is re-set to False (right?), so that's not gonna work...? The button-image changes once and then it stays the same picture.
 
I also tried this:

VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim CrossColor As Image
        Dim CrossBW As Image
        CrossColor = CheckList.My.Resources.Resources.cross_big
        CrossBW = CheckList.My.Resources.Resources.cross_big_bw1
        If Button2.BackgroundImage Is CrossBW Then
            Button2.BackgroundImage = CrossColor
        ElseIf Button2.BackgroundImage Is CrossColor Then
            Button2.BackgroundImage = CrossBW
        End If
    End Sub

But now the image on the button doesn't change at all...
 
OK I have solved my own problem:

VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Button2.BackgroundImage = CheckList.My.Resources.Resources.cross_big
        Button3.BackgroundImage = CheckList.My.Resources.Resources.check_big_bw1
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Button2.BackgroundImage = CheckList.My.Resources.Resources.cross_big_bw1
        Button3.BackgroundImage = CheckList.My.Resources.Resources.check_big
    End Sub

But now I'm still wondering why the first few things I tried didn't work??
Anyone?
 
I'm still wondering why the first few things I tried didn't work??
Because they IsNot the same image instance, even if they were loaded from same image source.
 
If you and me were clones and had the same name we would still be different persons, right? It's like that with the VB Image object and the named file in filesystem. Each time you request the image from resources it loads a new Image object from the specified name, so the same image data is used, but a new Image instance is created.
 
Back
Top