簡體   English   中英

C#從左到右移動標簽

[英]C# Move a Label from left to right

我想知道是否有使用計時器為標簽設置動畫的解決方案? 我希望label2緩慢向左移動,直到碰到label1並被擊中,然后從開始向左的位置向后退。 我試過了,但是當碰到label1時,它就停止了:

       label2.Location = new Point(label2.Location.X - 4, label2.Location.Y);

        if (label2.Location.X == label1.Location.Y)
        {
            label2.Location = new Point(label2.Location.X + 4, label2.Location.Y);
        }

您正在比較label2.Location.X == label1.Location.Y ,這似乎是一個錯字。

您需要一個方向變量,並且需要存儲Label2的原始位置,以便它知道要去哪里:

label2.Location = new Point(label2.Location.X + step, label2.Location.Y);
if (label2.Location.X <= label1.Location.X) {
  step = 4;
} else if (label2.Location.X >= originalX) {
  step = -4;
}

設置變量:

int step = -4;
int originalX;

然后使用Load Override方法設置originalX值:

protected override void OnLoad(EventArgs e) {
  base.OnLoad(e);
  originalX = label2.Location.X;
}

我認為您總是向左移動。 您不應該使用變量來跟蹤要移動的方向。

從這種意義上講,您需要檢查何時到達正確的邊距以再次更改方向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM