Hi all

Hi to move a label box from left to right and right to left with the help of timers.
Also how to change the fore color of the label (RAndom colcors )with the help of timers

## All 7 Replies

drag a timer on your form and a label double click on timer and you will have its event where you need to code.
now put one loop and condition in which you will define Left Position of Label and by condition you will check if label left position is 0 then move to right. That is all the theme behind your code. Plz try to code and if it doesnt work then send me the code at my email [email][email removed as per forum rules][/email]

Hi all

Hi to move a label box from left to right and right to left with the help of timers.
Also how to change the fore color of the label (RAndom colcors )with the help of timers

Use Rnd () function to Get the Random value.
The Rnd function returns a value less than 1, but greater than or equal to zero.

Example `Dim value As Integer = CInt(Int((256 * Rnd())))` You will get the value 0 to 255
By using Rnd() in timer, generate Color from Color.FromArgb() function and Set this for forecolor

Try this:

``````Public Class Form1
Dim RanNum As New Random            ' Random number generator
Dim DistMove As Integer = 10        ' The distance the label moves each time

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Left += DistMove
' Check if label has reach the right side of window
If Label1.Left + Label1.Width > Me.ClientRectangle.Width Then
' If so make sure none is hidden
Label1.Left = Me.ClientRectangle.Width - Label1.Width
' Reverse the direction - if positive make negative, if negative make positive
DistMove = Not DistMove
ElseIf Label1.Left < 0 Then
' If label is less than zero make it zero
Label1.Left = 0
' Reverse the direction
DistMove = Not DistMove
End If
' Change the forecolor of the label. The RanNum.Next gives a number between 0 and 255
Me.Label1.ForeColor = Color.FromArgb(255, RanNum.Next(0, 255), RanNum.Next(0, 255), RanNum.Next(0, 255))
End Sub
End Class``````
``````Public Class Form1

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Left = Label1.Left + 5
If Label1.Left >= Me.Width Then
Label1.Left = 0
Else
Label1.Left = Label1.Left + 5
End If

End Sub
End Class``````
``````Public Class Form1
' dont care about commented statements

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim a As Integer
' For a = 1 To 3
a = 1

'If lblmve.Left = lblmve.Left > Me.ClientRectangle.Width Then
'lblmve.Left = lblmve.Left + 10

'ElseIf lblmve.Left = lblmve.Left < Me.ClientRectangle.Width Then
'lblmve.Left = lblmve.Left - 10
'Else
'End If

'Next
' lblmve.Right = lblmve.Right + 10

lblmve.Left = lblmve.Left + 5 * a
If lblmve.Left >= Me.Width Then
a = -1
'lblmve.Left = 0
'ElseIf lblmve.Right >= Me.Width Then
'lblmve.Left = lblmve.Left - 100
End If
End Sub
End Class``````
``````Public Class Form1
Dim a As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
a = 1
End Sub

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

lblmve.Left = lblmve.Left + 5 * a
If lblmve.Left >= Me.Width Then
a = -1

End If
End Sub

End Class``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.