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

发布于 2025-01-05 09:40:58 字数 620 浏览 2 评论 0原文

我有一个标签,我正在尝试将其左右移动。我让它与 while (true) 循环一起工作,但决定尝试使用计时器。

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

据我所知,它可以正常工作,但是一旦抛出异常,

“计数”就必须是非负数。

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

I have a label that I am trying to move side to side. I got it to work with a while (true) loop but decided to try and use a timer instead.

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

that is as far as I have gotten it works sorta but after it does it once it throw the exception

'count' must be non-negative.

I am not sure how to fix this any help would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

万劫不复 2025-01-12 09:40:58

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

When y reaches 0, it will still be decremented one more time. Change to y > 0 and you should be ok.

感受沵的脚步 2025-01-12 09:40:58

当您传递负值作为第二个参数时,string() 构造函数会抛出异常。

MSDN:字符串构造函数(Char、Int32)

ArgumentOutOfRangeException - 计数小于零。

所以只需更改

if (y >= 0)

if (y > 0)

string() constructor throws when you are passing negative value as the second parameter.

MSDN: String Constructor (Char, Int32)

ArgumentOutOfRangeException - count is less than zero.

So just change

if (y >= 0)

to

if (y > 0)
梦幻的心爱 2025-01-12 09:40:58
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;
    }
}
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;
    }
}
〃温暖了心ぐ 2025-01-12 09:40:58
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;
    }
}
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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文