how to toggle button background colour on event click

clzanas

Member
Joined
Jul 15, 2009
Messages
16
Programming Experience
Beginner
hi guys,

may i know how to toggle a button background color when i click on another button?

That means i have two buttons on a window form and when i click on button A, button B will toggle its own background color based on the click event in button A.

Thnks in advance!!

regards,
Charles
 
You only need one button to toggle a Boolean value between True and False, using the logical NOT operator, and declaring it as a Static variable to remember the previous value. Here is a sample:


VB.NET:
    Private Sub btnToggle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToggle.Click
        Static onoff As Boolean
        onoff = Not onoff
        If onoff = True Then
            lblOnOff.Text = "ON"
            lblOnOff.BackColor = Color.Yellow
        Else
            lblOnOff.Text = "OFF"
            lblOnOff.BackColor = Color.Empty
        End If
    End Sub
 
Hi Solitaire,

Thnks for your help. I tried your code and its works. But i have another question. As for testing purpose, this one is for button event click. But may i know how to make button B's back ground colour interchange continuously when i click on button A.

Which means to say when i click on button A, button B will change its background colour between black and blue ( for example) in toggling mode.

thnks once again.

regards,
charles
 
To change continuously, you need to use a timer.

Other than that, simply replace the label back color with the button backcolor, and use the two colors you want instead of the sample I gave. I hope you don't need to have the code spoon-fed to you.
 
Hi Solitaire,

Thnks for the reply. I'm trying out the logic code to see if it works. If really cannot then i guess i'll have to pm u again . lol!

thnks!!

regards,

Charles
 
Back
Top