Click here to Skip to main content
15,883,773 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I have created a timer which makes a textbox's text appear to be moving from left to right, how can I make it look smoother?


So far it does what I want, but the movement doesn't look right.
Timer internal is 100

Code:

VB
Public Class Form1
    Dim CurrentName As String = String.Empty

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        CurrentName = "I'm looking for somehelp with this :-) Hope everyone's days going well"
        CurrentName = CurrentName + "          "
    End Sub

    Private Sub RotateTextbox_Tick(sender As Object, e As EventArgs) Handles RotateTextbox.Tick
        If Not CurrentName = String.Empty Then
                Dim FirstLetterCPN As String = CurrentName.Substring(0, 1)
                CurrentName = CurrentName.Substring(1, CurrentName.Length - 1) & FirstLetterCPN
        End If
        TextBox1.Text = CurrentName
    End Sub
End Class
Posted
Comments
Shahare 17-Feb-13 10:35am    
It will never look completely smooth if it text. Only using an image you can do it better.
Sergey Alexandrovich Kryukov 17-Feb-13 11:50am    
Not true at all. Just the opposite: if you do it properly, smoothness without image will be exactly as good or even better, because you save on performance. Please, your answer based on limited experience can confuse the beginners, so avoid it.
Don't forget that UI libraries also provide double buffering (optionally, or not).
—SA
Shahare 17-Feb-13 14:02pm    
Well my friend :) you are right, but...
From the question I understood he is a beginner probably first days of class.
If I would have gotten your answer to this question at his time I would be so confused, moreover this is a familiar question from homework.
What he has done is perfect for this.
Sergey Alexandrovich Kryukov 17-Feb-13 14:25pm    
No. Animation is not smooth. Why not answering a question correctly. I explained it all in my answer. :-)
Beginner or not is almost irrelevant. If a person is asking about this matter, she/he is supposed to be almost ready for solving the problem. Isn't it fair?
—SA
Sergey Alexandrovich Kryukov 17-Feb-13 11:53am    
I answered, but you did not tag your UI library or application type. When it comes to UI or graphics, you always should tag it.
—SA

First of all, I don't know what timer do you use. If you are using you are using System.Windows.Forms.Timer, don't do it: this timer is absolutely unsuitable for animation, because its accuracy is unacceptable, it can be amazingly bad. The only "benefit" of this timer class is the simplicity of its use: it does not require UI thread invocation.

Now, don' rely at exact time of the timer event at all, even with a better timer. Instead, measure "real" time immediately before rendering coordinates. Use System.Diagnostics.Stopwatch:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].

Calculate your coordinate based on measured time. In this case, even if the time intervals between "frames" are uneven, your motion will be smooth.

Better yet, don't use timers at all. Use additional thread with a cycle and System.Threading.Thread.Sleep calls inside a cycle, to setup some frame frequency. As you now will be measuring time in each frame anyway, exact timing does not matter much, but using a thread is way simpler.

Now, you will face the same problem with timers and thread: cross-thread calls to UI, so you have to solve it anyway. 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 vb.net[^],
Control events not firing after enable disable + multithreading[^].

Another important thing is to avoid flicker. For this purpose, you should use double buffering. You did not tag you UI library, but you can easily find how to do it in documentation.

—SA
 
Share this answer
 
v2
Comments
Andrews Smith 17-Feb-13 12:03pm    
You defiantly look to know what you're talking about and thanks for your help.The timer that I've used is the one from the toolbox.

Could you please show me an example how you would do this? (if that's too much work I understand) Its just what you're describing is currently beyond my understanding.
Sergey Alexandrovich Kryukov 17-Feb-13 12:13pm    
You should always show exact full type name, such as "System.Windows.Forms.Timer", especially when asking question. Why it's a problem to know? This is your code? The code generated by the designer? OK, look at it.
Unfortunately, the code sample will be too big for the forum. I have an unpublished article with such sample, but you have to wait (and translate from C#, which is not a big problem)...
You can try it by yourself and ask further question when and if you stuck...
—SA
Use marquee and put literal control. No need timer
 
Share this answer
 
Comments
Andrews Smith 17-Feb-13 12:03pm    
What do you mean? Also will this increase the smoothness?
Sergey Alexandrovich Kryukov 17-Feb-13 12:14pm    
No, forget it. This is not an adequate post. :-)
—SA

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