4

我使用一个标签,其中从文本框中输入的文本显示在该标签中。现在,我想让标签文本滚动。我通过互联网环顾四周,并尝试将其写入标签内的代码中:

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;
}

有人看到我做错了什么吗?

4

3 回答 3

6

//容易得多:

private void timer2scroll_Tick(object sender, EventArgs e)
{
  label10Info.Text = label10Info.Text.Substring(1, label10Info.Text.Length - 1) + label10Info.Text.Substring(0,1);
}
于 2014-11-11T17:32:41.457 回答
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();
}
于 2012-12-06T09:18:32.210 回答
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));
于 2016-05-19T22:55:17.413 回答