我正在使用标签,其中文本框中的文本输入显示在该标签中。现在,我想让标签文字滚动。我浏览了互联网,并尝试将其写入标签内的代码:
private void label1_Click(object sender, EventArgs e)
{
int Scroll;
string strString = "This is scrollable text...This is scrollable text...This is scrollable text";
Scroll = Scroll + 1;
int iLmt = strString.Length - Scroll;
if (iLmt < 20)
{
Scroll = 0;
}
string str = strString.Substring(Scroll, 20);
label1.Text = str;
}
有人看到我做错了吗?
答案 0 :(得分:4)
//更容易:
private void timer2scroll_Tick(object sender, EventArgs e)
{
label10Info.Text = label10Info.Text.Substring(1, label10Info.Text.Length - 1) + label10Info.Text.Substring(0,1);
}
答案 1 :(得分:2)
您需要在函数调用之外声明Scroll变量,每次单击它时都会重置。
在这里,表单加载时带有计时器的代码自动滚动文本:
private Timer tmr;
private int scrll { get; set; }
void Form1_Load(object sender, EventArgs e)
{
tmr = new Timer();
tmr.Tick += new EventHandler(this.TimerTick);
tmr.Interval = 200;
tmr.Start();
}
private void TimerTick(object sender, EventArgs e)
{
ScrollLabel();
}
private void ScrollLabel()
{
string strString = "This is scrollable text...This is scrollable text...This is scrollable text";
scrll = scrll + 1;
int iLmt = strString.Length - scrll;
if (iLmt < 20)
{
scrll = 0;
}
string str = strString.Substring(scrll, 20);
label1.Text = str;
}
private void label1_Click(object sender, EventArgs e)
{
ScrollLabel();
}
答案 2 :(得分:0)
这可以使用我的库。
WinForm动画库[.Net3.5 +]
一个简单的库,用于在.Net WinForm(.Net中)中设置控件/值的动画 3.5及以后)。基于关键帧(路径)并可完全自定义。
https://falahati.github.io/WinFormAnimation/
var textToScroll = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
var durationOfAnimation = 5000ul;
var maxLabelChars = 20;
var label = label1;
new Animator(new Path(0, 100, durationOfAnimation))
{
Repeat = true,
ReverseRepeat = true
}.Play(
new SafeInvoker<float>(f =>
{
label.Text =
textToScroll.Substring(
(int) Math.Max(Math.Ceiling((textToScroll.Length - maxLabelChars)/100f * f) - 1, 0),
maxLabelChars);
}, label));