[英]c# Moving label from right to left
我在C#Windows窗体中有此代码。 此代码使标签文本从右向左移动。 但是,当文本消失时,它就不会从右到左再次出现。
label21.Location = new Point(label21.Location.X - 15, label21.Location.Y);
if (label21.Location.X > this.Width)
{
label21.Location = new Point(0 - label21.Width, label21.Location.Y);
}
这样做:
label21.Location.X - 15
您将标签向左移动。
但是X > this.Width
检查X
是否大于右侧(向左移动是不可能的)。
您的支票应为:
if (label21.Location.X + label21.Width < 0)
{
label21.Location = new Point(this.Width, label21.Location.Y);
}
代码中有一个错字, label1
应该是label21
。
对其进行如下纠正:
label21.Location = new Point(label21.Location.X - 15, label21.Location.Y);
if (label21.Location.X > this.Width)
{
label21.Location = new Point(0 - label21.Width, label21.Location.Y);
}
this.Width
是包含控件的“最右边的点”。 由于您向左移动,因此标签的位置X朝向0,而不是this.Width。
如果标签完全移到左侧,则标签消失了,并且从一开始就从右侧开始。
Point a = new Point(this.Width, label4.Location.Y);
Point b = new Point(label4.Location.X-10, label4.Location.Y);
if (label4.Location.X < 0-label4.Width+1)
{
label4.Location = a;
}
else
{
label4.Location = b;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.