?
Solved

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

Posted on 2008-11-15
15
Medium Priority
?
711 Views
Last Modified: 2013-11-26
Dear Experts:

Hi!

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

Thanks a lot!
0
Comment
Question by:duta
  • 7
  • 3
  • 2
  • +2
15 Comments
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 200 total points
ID: 22968474
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 total points
ID: 22968478
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
 

Author Comment

by:duta
ID: 22968541
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 400 total points
ID: 22968644
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
 

Author Comment

by:duta
ID: 22968753
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22968778
>>Still I get an error message over "Handles Timer1.Tick".

You need to add a Timer control to your form.
0
 

Author Comment

by:duta
ID: 22968840
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
 

Author Comment

by:duta
ID: 22968844
I guess I need to drag and drop a timer from toolbox?
0
 

Author Comment

by:duta
ID: 22968862
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 22968914
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
 

Author Comment

by:duta
ID: 22968969

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
 
LVL 48

Accepted Solution

by:
jpaulino earned 1200 total points
ID: 22969050
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
 

Author Comment

by:duta
ID: 22969090
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22969137
Thanx for helping him with the final pieces jpaulino...  =)
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22970074
I always like to help Idle_Mind, and was only like you said it: "final pieces " :)
Great weekend for all!
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question