I came up with the idea of making a label scroll a word to one side and then change the word and scroll back to the other like so
"ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping"
" pong"
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
"pong "
I want it to do ^^ only in a constant loop but I don't know how I would even get started doing that I would REALLY appreciate it if someone could help me with this. The max length of the text has to be 15 characters.
I don't care if it is smooth scrolling.
I want it to be a Winforms application and use .Net framework 4.0.
Make a for-loop that runs from 0 to 11 (15 - length of "ping"). With new String(' ', i)
you can create a string that is i
spaces long. Then set the Text of your label to this space string concatenated with the word "ping".
Now you can make another loop, running from 11 down to 0 doing the same but with the word "pong".
If you enclose both loops in an endless loop (while (true) { ... }
) this will run indefinitely.
You also might want to add a pause each time after you set the label text with Thread.Sleep(200)
. Where you specify the time is in milliseconds.
EDIT (since it is not homework):
Go to the events tab in the properties window and add a Shown
event handler
private void frmMarquee_Shown(object sender, EventArgs e)
{
while (true) {
for (int i = 0; i <= 11; i++) {
label1.Text = new String(' ', i) + "ping";
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
for (int i = 11; i >= 0; i--) {
label1.Text = new String(' ', i) + "pong";
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
}
}
Note, this solution is not perfect, as the form will not close properly. You will have to abort the program. A solution using a timer will work smoother and the form will behave as expected when closing, however this is a straightforward and simple solution.