Click here to Skip to main content
15,877,384 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I want to move the labels smoothly. Here, is my codes (
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Label4.Visible = True
    Label4.Top = Label4.Top - 75
    Label3.Visible = False
    Timer1.Enabled = False
    Timer2.Enabled = True
End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    Label4.Top = Label4.Top + 75
    Label3.Visible = True
    Label4.Visible = False
    Timer1.Enabled = True
    Timer2.Enabled = False
End Sub

). It goes very roughly. I want to make it go very smoothly. In windows 8 metro screen, see the things going up either down. It goes very smoothly help me with this. Thanks in advance.
Sergey Alexandrovich Kryukov 9-Oct-14 0:38am    
Did you try to move by 1 pixel or so?
By the way, what is your application type and UI library you use.
Why do you repeat code and probably use two timers? (Code looks bad, really.)
JoshuaDsouza19 9-Oct-14 0:39am    
just trying to create a loop
Sergey Alexandrovich Kryukov 9-Oct-14 0:43am    
JoshuaDsouza19 9-Oct-14 0:39am    
my application type is windows form
and where do i see my ui library
Sergey Alexandrovich Kryukov 9-Oct-14 0:44am    
I demonstrated animation like that in one of my answers, nobody complained about lack of smoothness.

1 solution

First of all, never use System.Windows.Forms.Timer for animation, use one of the other timers. Even better and simpler, don't use a timer, use a separate thread. If you need to know real time of the move, use System.Diagnostics.Stopwatch (you only need relative time). Make smaller steps.

Now, with all the timers (except the one I mentioned above which cannot give you any acceptable accuracy) and with a separate thread, you will need to delegate your method changing anything on your UI to the UI thread. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in[^],
Control events not firing after enable disable + multithreading[^].

Write code accurately; never ever use auto-generated names like Label4 or Timer1_Tick; they violate (good) Microsoft naming conventions and not indented for permanent use; rename them all to give semantically sensible names, otherwise you code will be hard to maintain.

Basically, that's all you need.

Share this answer
Maciej Los 9-Oct-14 1:59am    
Sergey Alexandrovich Kryukov 9-Oct-14 2:00am    
Thank you, Maciej.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900