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
Thanks in advance
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
Thanks in advance
Consolidating the thread: a robust WinForms pattern is to keep one persistent Random instance, track direction with a signed integer (1 or -1), and test the label against Me.ClientSize so the label remains fully visible. This builds on suggestions from and and corrects the common reversal mistake seen in 's snippet (bitwise Not on a numeric step). The example below changes color on bounce and also shows how to reduce flicker. (If the environment is ASP.NET rather than WinForms, animate client-side with JavaScript/CSS instead of a server Timer.)
Public Class Form1
Private moveStep As Integer = 4
Private direction As Integer = 1 ' 1 = right, -1 = left
Private rng As New Random()
Private colorTick As Integer = 0
Private colorEvery As Integer = 6
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.AllPaintingInWmPaint, True)
Me.UpdateStyles()
Label1.AutoSize = False
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Left += moveStep * direction
If Label1.Right >= Me.ClientSize.Width Then
Label1.Left = Me.ClientSize.Width - Label1.Width
direction = -1
ApplyRandomColor()
ElseIf Label1.Left <= 0 Then
Label1.Left = 0
direction = 1
ApplyRandomColor()
End If
colorTick += 1
If colorTick >= colorEvery Then
ApplyRandomColor()
colorTick = 0
End If
End Sub
Private Sub ApplyRandomColor()
Label1.ForeColor = Color.FromArgb(rng.Next(256), rng.Next(256), rng.Next(256))
End Sub
End Class Notes: keep the Random instance at form scope (creating it each Tick causes repeated/identical colors), use direction = -direction to flip movement, set Label1.AutoSize = False when a stable width is desired, and tweak Timer.Interval plus moveStep for smoothness. If flicker persists, double buffering (shown) or custom drawing in OnPaint gives the best results.
Jump to Post— Alekhan 0drag 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 …
Jump to Post— selvaganapathy 31Use 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 …
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
Thanks in advance
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
' Add distance to label
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 We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.