VB 2005: How to make label change in color at every 2 seconds?

Dear Experts:

Hi!

I wonder whether there is a way to make label to change color periodically (say, every 2 seconds).

Thanks a lot!
dutaAsked:
Who is Participating?
 
Jorge PaulinoConnect With a Mentor IT Pro/DeveloperCommented:
Attention: don't forget I'm just trying to give some help BUT other experts have showed you the solution!
It don't need to create a new instance of the control timer, since you have already dropped from the toolbox.
 
Two ways:

' Method #1
' Dropping a timer from the toolbox
' 
Public Class frmcalculate
 
    Private randNumber As New Random
 
    Private Sub frmForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 2000
        Timer1.Start()
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim r As Integer = randNumber.Next(0, 254)
        Dim g As Integer = randNumber.Next(0, 254)
        Dim b As Integer = randNumber.Next(0, 254)
        lblTitle.ForeColor = Color.FromArgb(r, g, b)
    End Sub
 
End Class
 
 
 
' Method #2
' Using only code 
'
Public Class frmcalculate
 
    Private WithEvents tm As New Timer
    Private randNumber As New Random
 
    Private Sub frmForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tm.Interval = 2000
        tm.Start()
    End Sub
 
    Private Sub tm_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tm.Tick
        Dim r As Integer = randNumber.Next(0, 254)
        Dim g As Integer = randNumber.Next(0, 254)
        Dim b As Integer = randNumber.Next(0, 254)
        lblTitle.ForeColor = Color.FromArgb(r, g, b)
    End Sub
 
End Class 

Open in new window

0
 
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
add a timer to your form, set the Interval to 2000 and Enabled to True. In the Tick event of the Timer, change the color of your label.
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Yes...drop a Timer control on your Form, set the Interval Property to 2000, and the Enabled Property to True:



Public Class Form1
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.ForeColor = IIf(Label1.ForeColor.Equals(Color.Red), Color.Yellow, Color.Red)
    End Sub
 
End Class

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
dutaAuthor Commented:
I got an error message: error BC30287: '.' expected,   over Timer1.tick in the code below.

Thanks a lot!

______________________________________________________________________________

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        lblWarning.ForeColor = IIf(lblWarning.ForeColor.Equals(Color.Red), Color.Yellow, Color.Red)
0
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi duta,

Here is a code snippet to show how it can be done. Drop a Timer control onto the form.

Regards,
Fernando
Public Class Form1 
    Dim randNumber As Random 
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim r As Integer = randNumber.Next(0, 254)
        Dim g As Integer = randNumber.Next(0, 254)
        Dim b As Integer = randNumber.Next(0, 254)
        Label1.BackColor = Color.FromArgb(r, g, b)
    End Sub 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        randNumber = New Random(DateTime.Now().TimeOfDay.Seconds)
        Timer1.Interval = 2000
        Timer1.Enabled = True
    End Sub 
End Class

Open in new window

0
 
dutaAuthor Commented:
Still I get an error message over "Handles Timer1.Tick".

Thanks!

______________________________________________________________
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim r As Integer = randNumber.Next(0, 254)
        Dim g As Integer = randNumber.Next(0, 254)
        Dim b As Integer = randNumber.Next(0, 254)
        lblWarning.BackColor = Color.FromArgb(r, g, b)
    End Sub
    Private Sub frmForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        randNumber = New Random(DateTime.Now().TimeOfDay.Seconds)
        Dim Timer As New Timer
        Timer.Interval = 2000
        Timer.Enabled = True
    End Sub
0
 
Éric MoreauSenior .Net ConsultantCommented:
>>Still I get an error message over "Handles Timer1.Tick".

You need to add a Timer control to your form.
0
 
dutaAuthor Commented:
You said, You need to add a Timer control to your form

What do you mean by that?

I am so sorry for ignorance.

0
 
dutaAuthor Commented:
I guess I need to drag and drop a timer from toolbox?
0
 
dutaAuthor Commented:
I dragged and dropped a Timer from Toolbox.
And I copied and pasted the code you gave under the Timer1_Click  sub procedure.

But I saw a blue wavy lines around: randNumber.Next(0, 254)
It seems that I need to declare randNumber?

Thanks!
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Since you choose FernandoSoto exemple you have to declare outside of the sub scope
Dim randNumber As Random  
and then in the form load
randNumber = New Random(DateTime.Now().TimeOfDay.Seconds)
0
 
dutaAuthor Commented:

I don't see any error, but the color of label (lblWarning) still does not change.

Thank you for your patience!

_________________________________________________
Public Class frmcalculate


    Dim randNumber As Random
.
.
.
.

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim r As Integer = randNumber.Next(0, 254)
        Dim g As Integer = randNumber.Next(0, 254)
        Dim b As Integer = randNumber.Next(0, 254)
        lblTitle.BackColor = Color.FromArgb(r, g, b)
    End Sub

    Private Sub frmForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        randNumber = New Random(DateTime.Now().TimeOfDay.Seconds)
        Dim Timer As New Timer
        Timer.Interval = 2000
        Timer.Enabled = True


    End Sub

End Class
0
 
dutaAuthor Commented:
Thank you so much for your patience and great tip.

I wish I may award many more points to you.

Thanks all of you geniuses!

duta
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Thanx for helping him with the final pieces jpaulino...  =)
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
I always like to help Idle_Mind, and was only like you said it: "final pieces " :)
Great weekend for all!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.