如何使标签滚动文字来回? [英] How to make a label scroll a word back and forth?
问题描述
我想出了制作一个标签滚动字一侧,然后改变其词,像这样
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 "
我希望它做的^^只在一个恒定的循环,但我不知道我怎么会甚至开始这样做,我会很感激,如果有人可以帮助我。文本的最大长度必须是15个字符。
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.
我想这是一个WinForms应用程序并使用框架4.0。
I want it to be a Winforms application and use .Net framework 4.0.
推荐答案
请for循环运行从0到11(15 - 的平长度)。随着新的字符串('',我)
您可以创建一个字符串,它是 I
空格的长。然后设置标签的这个空间字符串串联用平的文字。
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".
现在你可以让另一个循环,从11运行到0做同样的但用傍。
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.
您可能还需要您设置与发。睡眠(200)
。在哪里您指定的时间以毫秒为单位。
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.
这篇关于如何使标签滚动文字来回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!