2012-08-09 75 views
-2

我正在使用此代码以制作从右向左滚动的选取框标签。这个选取框代码为什么不起作用?

private int xPos = 651; 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     if (this.Width == xPos) 
     { 
      //repeat marquee 
      xPos = 651; 
      this.Label7.Location = new System.Drawing.Point(651, 334); 
      xPos--; 
     } 
     else 
     { 
      this.Label7.Location = new System.Drawing.Point(xPos, 334); 
      xPos--; 
     } 
    } 

651是窗体的宽度。

此代码使标签从右向左移动,滚动窗体应该像它一样,但不会再次在右侧重新启动。

+3

双击标题:) – RedFilter 2012-08-09 18:00:43

+0

负只是出于好奇:为什么不这样做字幕上使用jQuery或普通的JavaScript客户端? – Dimitri 2012-08-09 18:01:22

+0

@Dimitri,我认为这是winform,我做了太多的webform :-p – Fredou 2012-08-09 18:01:53

回答

0

我假设this.Width是一个正值,因此如果你正在做重复减法,你只能在开始时达到这个值。 尝试if (xPos == 0)代替

0

,对我的代码没有什么意义我

尝试此相反,调整它就像你想要的。

private bool goLeft; 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     if (Label7.Width + Label7.Left >= this.Width) 
     { 
      goLeft = true; 
     } 
     else if (Label7.Left < 0) 
     { 
      goLeft = false; 
     } 

     Label7.Left += goLeft ? -10 : 10; 
    } 
0

虽然我同意,它会更容易只使用来自客户端的字幕,这不是他问什么......

您在this.Width和减量开始XPOS所以,当它到达0而不是this.Width时,你需要重置xPos。每次运行的第一次迭代时,xPos仅等于this.Width。

private int xPos = this.Width; 

private void timer1_Tick(object sender, EventArgs e) { 
    if (xPos == 0) { xPos = this.Width; } 
    this.Label7.Location = new System.Drawing.Point(xPos, 334); 
    xPos--; 
} 
+0

这是有效的,但是有一种方法可以调整它,以便在标签的末尾碰到表单左侧而不是开始时重置? – user1588364 2012-08-09 18:42:19

+0

而不是开始什么? – Pacane 2012-08-10 11:52:08

相关问题