如何使用计时器使文本从一侧移动到另一侧?

时间:2012-02-14 15:26:38

标签: c# winforms timer label marquee

我有一个标签,我试图左右移动。我让它使用了一段时间(真实)循环,但决定尝试使用计时器。

    private int x = 0;
    private int y = 11;
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (x <= 11)
        {
            x++;
            string ping = new string(' ', x) + "ping";
            label2.Text = ping;
        }
        else if (y >= 0)
        {
            y--;
            string pong = new string(' ', y) + "pong"; // this is where the exceptions given
            label2.Text = pong;
        }

就我所知它而言,它的工作方式有点但在它抛出异常之后就会这样做

'count'必须是非负数。

我不知道如何解决这个问题,任何帮助都会很棒。

4 个答案:

答案 0 :(得分:1)

当您将负值作为第二个参数传递时,

string()构造函数抛出。

MSDN:String Constructor (Char, Int32)

  

ArgumentOutOfRangeException - count小于零。

所以只需改变

if (y >= 0)

if (y > 0)

答案 1 :(得分:1)

y达到0时,它仍然会再次递减。更改为y > 0,您应该没问题。

答案 2 :(得分:0)

private int x = 0;
private int y = 100;
private void timer1_Tick(object sender, EventArgs e)
{
    if (x <= 100)
    {
        x++;
        string ping = new string(' ', x) + "COURT DOCUMENT MANAGEMENT SYSTEM";
        label1.Text = ping;
    }
    else if (y > 0)
    {
        y--;
        string pong = new string(' ', y) + "MY ARCHIVE MY LIFELINE!!!!"; // this is where the exceptions given
        label2.Text = pong;
    }
}

答案 3 :(得分:0)

if (x <= 11)
{
    x++;
    string ping = new string(' ', x) + "ping";
    label2.Text = ping;
    if (x == 11)
    {
        y = 11;
    }
}
else if (y >= 0)
{
    y--;
    string pong = new string(' ', y) + "pong"; // this is where the exceptions given
    label2.Text = pong;
    if (y == 0)
    {
        x = 0;
    }
}