Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#将标签从右向左移动_C#_Winforms - Fatal编程技术网

c#将标签从右向左移动

c#将标签从右向左移动,c#,winforms,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.Locatio

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

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

此.Width
是包含控件的“最右侧点”。 由于向左移动,标签的位置X朝0移动,而不是朝这个.Width移动。

执行此操作:

label21.Location.X - 15
将标签向左移动

但是
X>这个.Width
检查
X
是否大于右侧(向左移动时不可能)

您的支票应为:

if (label21.Location.X + label21.Width < 0)
{
     label21.Location = new Point(this.Width, label21.Location.Y);  
}
if(label21.Location.X+label21.Width<0)
{
label21.Location=新点(this.Width,label21.Location.Y);
}
这就可以了 若标签完全进入左边,那个么它就会消失,并从右边开始

        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;
        }
点a=新点(此点为.Width,标签4.Location.Y);
点b=新点(label4.Location.X-10,label4.Location.Y);
if(label4.位置X<0-label4.宽度+1)
{
标签4.位置=a;
}
其他的
{
标签4.位置=b;
}

label1。使用宽度
代替
label21。宽度
@AlfieGoodacre是对的。您正在使用另一个标签的宽度重置标签的位置。不过,使用
-label21.Width
就足够了。在取消显示之前最初显示的文本是什么?我只是在这里键入了错误的内容。。我的原始代码中有正确的标签21。还是一样的问题我的标签文本是“这里有东西……”我只是在这里打错了。。我的原始代码中有正确的标签21。还是一样的问题,你能解释一下吗?我能做点什么吗?我试过你的代码。标签文本会移动,但不会消失。就在它击中左侧时,它又从右侧开始。@Sinanlablogranahendi,你想让标签在左侧消失然后出现在右侧吗?是的:)@Sinanlablogranahendi请看编辑后的答案:
if(label21.Location.X+label21.Width<0)
@Sinanlablogranahendi,看编辑后的:
label21.Location=new Point(此参数为.Width,label21.Location.Y);