2012-02-14 16 views
1

私は横に動こうとしているラベルがあります。私はwhile(true)ループで動作するようにしましたが、代わりにタイマーを試してみることにしました。テキストを左右に移動させるタイマーの使い方は?

限り、私はそれがみかん動作しますが、それは例外

「回数」を投げたら、それはそれをしないの後に非負でなければなりません。得ているようです

private int x = 0; 
    private int y = 11; 
    private void timer1_Tick(object sender, EventArgs e) 
    { 
     if (x <= 11) 
     { 
      x++; 
      string ping = new string(' ', x) + "ping"; 
      label2.Text = ping; 
     } 
     else if (y >= 0) 
     { 
      y--; 
      string pong = new string(' ', y) + "pong"; // this is where the exceptions given 
      label2.Text = pong; 
     } 

この問題を解決する方法がわかりません。

答えて

1

if (y >= 0) 

を変更。 y > 0に変更してください。あなたは大丈夫です。

1

string() 2番目のパラメータとして負の値を渡すときにコンストラクタがスローされます。

MSDN:String Constructor (Char, Int32)

例外ArgumentOutOfRangeException - カウントがゼロ未満です。

だからyが0になると、それはまだ1つのより多くの時間を減らされます

if (y > 0) 
0
private int x = 0; 
private int y = 100; 
private void timer1_Tick(object sender, EventArgs e) 
{ 
    if (x <= 100) 
    { 
     x++; 
     string ping = new string(' ', x) + "COURT DOCUMENT MANAGEMENT SYSTEM"; 
     label1.Text = ping; 
    } 
    else if (y > 0) 
    { 
     y--; 
     string pong = new string(' ', y) + "MY ARCHIVE MY LIFELINE!!!!"; // this is where the exceptions given 
     label2.Text = pong; 
    } 
} 
0
if (x <= 11) 
{ 
    x++; 
    string ping = new string(' ', x) + "ping"; 
    label2.Text = ping; 
    if (x == 11) 
    { 
     y = 11; 
    } 
} 
else if (y >= 0) 
{ 
    y--; 
    string pong = new string(' ', y) + "pong"; // this is where the exceptions given 
    label2.Text = pong; 
    if (y == 0) 
    { 
     x = 0; 
    } 
} 
+1

あなたはあなたのコードの説明を提供する必要があります:) –

関連する問題