c#从右向左移动标签

时间:2017-05-25 11:21:40

标签: c# winforms

我在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);  
}

4 个答案:

答案 0 :(得分:2)

这样做:

label21.Location.X - 15

将标签移到左侧。

X > this.Width检查X是否超过右侧(向左移动时不可能)。

您的支票应该是:

if (label21.Location.X + label21.Width < 0)
{
     label21.Location = new Point(this.Width, label21.Location.Y);  
}

答案 1 :(得分:1)

代码中有拼写错误,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);  
}

答案 2 :(得分:1)

this.Width是&#34;最右边的点&#34;包含控件。 由于您向左移动,因此标签的位置X将朝向0而不是朝向此。宽度。

答案 3 :(得分:0)

这可以做的工作 如果标签完全进入左侧,那么它就会消失并从头开始。

        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;
        }
相关问题