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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .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 TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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
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
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Fernando SotoRetiredCommented:
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
Jorge PaulinoIT 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.