Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Button Color upon Click

Status
Not open for further replies.

Trex2303

Technical User
Oct 23, 2007
3
0
0
US
Using VB 2005 EE....

Im creating a lil program that sort of goes along with a lil game... What im trying to do is when you click the button once, it changes color.. twice - another color, 3 times, Yep - another color lol..... and if you clicka 4th time, back to its original state...

I have the following set up so far:
Code:
        'First click turns Red '
        Button1.BackColor = Color.Red



        'Second click turns Blue'
        Button1.BackColor = Color.Blue



        'Third click turns Gold'
        Button1.BackColor = Color.Gold

and Of course when you click it - it just goes straight to gold. I cant figure out what I need in between each color to get it to change "per click".

can anyone help me?

Thanx
 
Set a variable in the module level like..
Code:
    Dim ClickCount As Integer = 1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not ClickCount = 3 Then
            ClickCount += 1
        Else
            ClickCount = 1
        End If
        Select Case ClickCount
            Case Is = 1
                Button1.BackColor = Color.Red
            Case Is = 2
                Button1.BackColor = Color.Blue
            Case Is = 3
                Button1.BackColor = Color.Gold
        End Select

    End Sub

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
That's the stuff from Zameer, I'd just change it slightly to turn it red on the first click first time through. You can also add an extra case in to set the button colour back to it's original on the fourth click. Something like (using Zameer's code as the base):
Code:
Dim ClickCount As Integer = 0

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      If Not ClickCount = 3 Then
         ClickCount += 1
      Else
         ClickCount = 0
      End If
      Select Case ClickCount
         Case Is = 0
            Button1.BackColor = Color.LightGray ' or your default colour
         Case Is = 1
            Button1.BackColor = Color.Red
         Case Is = 2
            Button1.BackColor = Color.Blue
         Case Is = 3
            Button1.BackColor = Color.Gold
      End Select

   End Sub
Hope this helps

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Yep, I'll do it. There you go Zameer.

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Thanks for the pinky.. Barney..

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
[wink]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top