Click here to Skip to main content
15,946,179 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I want to change a label to three different colors according to the timer control.
The code I have so far is:
VB
If Label1.ForeColor = Color.Orange Then
    Label2.ForeColor = Color.White
    Label3.ForeColor = Color.Green
ElseIf Label2.ForeColor = Color.Orange Then
    Label1.ForeColor = Color.Green
    Label3.ForeColor = Color.White
ElseIf Label3.ForeColor = Color.White Then
    Label2.ForeColor = Color.Green
    Label3.ForeColor = Color.Orange
End If


Please help me with the basic stuff.
Posted
Updated 18-Mar-11 5:02am
v3

All you have to do is include a Timer in your program.
You can do it using the designer (drag it from the tool box and set the properties) or in code:
Private Shared WithEvents changeColour As New System.Windows.Forms.Timer()
In your Form Load event:
changeColour.Interval = 500 ' Half a second
changeColour.Start()

And add the handler:
Private Shared Sub changeColour_tick(myObject As [Object], myEventArgs As EventArgs) _
    Handles myTimer.Tick
       If Label1.ForeColor = Color.Orange Then
           Label2.ForeColor = Color.White
           Label3.ForeColor = Color.Green
       ElseIf Label2.ForeColor = Color.Orange Then
           Label1.ForeColor = Color.Green
           Label3.ForeColor = Color.White
       ElseIf Label3.ForeColor = Color.White Then
           Label2.ForeColor = Color.Green
           Label3.ForeColor = Color.Orange
       End If
End Sub


[edit]Posted a bit of cr@p VB code - I just did a verbatim translation from C# and forgot how crippled VB is... = OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
sima2011 18-Mar-11 11:00am    
ya i done it
If Label1.BackColor = Color.Green Then
Label1.BackColor = Color.White
Label2.BackColor = Color.Green
Label3.BackColor = Color.Orange
ElseIf Label1.BackColor = Color.White Then
Label1.BackColor = Color.Orange
Label2.BackColor = Color.White
Label3.BackColor = Color.Green
ElseIf Label1.BackColor = Color.Orange Then
Label1.BackColor = Color.Green
Label2.BackColor = Color.Orange
Label3.BackColor = Color.White
End If
VB
Dim xi As Integer = 0
    Dim yi As Integer = 0
    Dim zi As Integer = 0
<pre lang="vb">Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If xi &lt; 250 Then
            xi = xi + 10
        End If

        If xi = 250 Then
            If yi &lt; 250 Then
                yi = yi + 10
            End If
            If yi = 250 Then
                zi = zi + 10
                If zi = 250 Then
                    zi = 0
                    yi = 0
                    xi = 0
                End If
            End If
        End If
        Label8.ForeColor = ColorTranslator.FromOle(RGB(xi, yi, zi))
        Label9.ForeColor = ColorTranslator.FromOle(RGB(zi, yi, xi))
        Label10.ForeColor = ColorTranslator.FromOle(RGB(yi, xi, zi))
        Label11.ForeColor = ColorTranslator.FromOle(RGB(xi, zi, yi))


    End Sub</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900