如何使用计时器使文本从一侧移动到另一侧?
我有一个标签,我正在尝试将其左右移动。我让它与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当
y
达到0时,它仍然会再减1次。更改为y > 0
你应该没问题。When
y
reaches 0, it will still be decremented one more time. Change toy > 0
and you should be ok.当您传递负值作为第二个参数时,
string()
构造函数会抛出异常。MSDN:字符串构造函数(Char、Int32)
所以只需更改
为
string()
constructor throws when you are passing negative value as the second parameter.MSDN: String Constructor (Char, Int32)
So just change
to