【发布时间】:2015-11-19 05:43:43
【问题描述】:
背景:首先我知道我的一些代码很乱,请不要讨厌我。基本上,我想为 Windows 窗体窗口上的 TextBox 对象的移动设置动画。我设法通过使用Timer 做到了这一点。但是,我的其余代码在计时器运行时执行(因此 TextBox 仍在移动)。我该如何阻止这种情况发生。
这是我的一些代码:
move方法:
private void move(int x, int y)
{
xValue = x;
yValue = y;
// Check to see if x co-ord needs moving up or down
if (xValue > txtData.Location.X) // UP
{
xDir = 1;
}
else if (xValue < box.Location.X) // DOWN
{
xDir = -1;
}
else // No change
{
.xDir = 0;
}
if (yValue > box.Location.Y) // RIGHT
{
yDir = 1;
}
else if (yValue < Location.Y) // LEFT
{
yDir = -1;
}
else // No change
{
yDir = 0;
}
timer.Start();
}
定时器Tick方法:
private void timer_Tick(object sender, EventArgs e)
{
while (xValue != box.Location.X && yValue != box.Location.Y)
{
if (yDir == 0)
{
box.SetBounds(box.Location.X + xDir, box.Location.Y, box.Width, box.Height);
}
else
{
box.SetBounds(box.Location.X, box.Location.Y + yDir, box.Width, box.Height);
}
}
}
move 来电:
move(478, 267);
move(647, 267);
move(647, 257);
【问题讨论】:
-
你不想在进入while循环之前禁用计时器,当所有任务都完成后,然后将计时器设置为启用..
-
“我的其余代码在计时器运行时执行”——剩下的代码是什么?看来您正在使用
System.Windows.Forms.Timer(.NET 中有许多Timer类),如果是这样,则在while循环在Tick事件处理程序中执行时不会发生任何其他事情(甚至不更新窗口的视觉外观)。目前尚不清楚哪些其他代码可能正在运行,这将是一个问题。鉴于此处发布的代码,您似乎遇到了相反的问题(即没有其他代码会运行) -
请提供可靠地重现您的问题的a good, minimal, complete code example。准确而具体地说明该代码的功能以及它与您希望它做什么的不同之处。
-
我希望它使用 478、267 参数和 THEN 647、267 参数运行 while 循环。