2016-12-12 11 views
2

にバウンス私は現在、ヘッドファーストC#第三版を読んでいる、と私はIDEとしてのVisual Studio 2015を使用しています。本の中のプログラムの一つは、ラベルをバウンスと呼ばれています。C#のフォームの片側から他の

希望の結果:プログラムでは、3つのラベルオブジェクトの配列がフォーム上のボタンに対応しています。ラベル各ボタンの各々が押下された場合、ラベルは、次いで、「バウンス」のような他に、フォームの一端から移動しなければなりません。

問題:ラベルはフォームの右端に移動し、フォームの3/4を停止します。彼らは決して跳ね返ることはない。画像のリンクを参照してください。

https://postimg.org/image/qn7s9pfdz/

いくつかの技術的な詳細(担当者は、インライン化するには低すぎる):フォームは、タイマーを持っている、それがに有効で、1タイマーの間隔をすると仮定されていますサイクル警備員の配列、および彼らがnullでないならば、彼らのmoveメソッドを呼び出します。

私は、学習している本の中のページへのリンクを含んでいます。

https://books.google.com/books?id=mcWDAAAAQBAJ&pg=PA181&lpg=PA181&dq=bouncing+labels+c%23&source=bl&ots=U219enVOgQ&sig=9joHw7tNrCcM8d6rLZDR1ydFsBA&hl=en&sa=X&ved=0ahUKEwiizKqZw-7QAhUFWSYKHQnXAmQQ6AEINDAE#v=onepage&q=bouncing%20labels%20c%23&f=false

私はバウンサークラスのスクリプト、およびフォームを持っています。

バウンサークラス

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Bouncinglabels 
{ 
    using System.Windows.Forms; 
    class LabelBouncer 
    { 
     public Label MyLabel; 
     public bool GoingForward = true; 

     public void Move() 
     { 
      if (MyLabel != null) 
      { 
       if (GoingForward == true) 
       { 
        MyLabel.Left += 5; 
       } 
       if (MyLabel.Left >= MyLabel.Parent.Width - MyLabel.Width) 
       { 
        GoingForward = false; 
       } 
      } 
      else 
      { 
       MyLabel.Left -= 5; 
       if (MyLabel.Left <= 0) 
       { 
        GoingForward = true; 
       } 
      } 
     } 
    } 
} 

ここでこれは動作するはずのForm1.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Bouncinglabels 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     LabelBouncer[] bouncers = new LabelBouncer[3]; 

     private void ToggleBouncing(int index, Label labelToBounce) 
     { 
      if (bouncers[index]==null) 
      { 
       bouncers[index] = new LabelBouncer(); 
       bouncers[index].MyLabel = labelToBounce; 
      } 
      else 
      { 
       bouncers[index] = null; 
      } 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      ToggleBouncing(0, label1); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      ToggleBouncing(1, label2); 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 

      ToggleBouncing(2, label3); 
     } 
     private void timer1_Tick(object sender, EventArgs e) 
     { 
      for (int i = 0; i < 3; i++) 
      { 
       if (bouncers[i] != null) 
       { 
        bouncers[i].Move(); 
       } 
      } 
     } 


    } 
} 

答えて

0

です。ちょうどあなたのelse文を動かしました。あなたは、mylabelという!= nullを指定して、あなたの場合は、以下のあなたの他を持っていました。

namespace Bouncinglabels 
{ 
using System.Windows.Forms; 
class LabelBouncer 
{ 
    public Label MyLabel; 
    public bool GoingForward = true; 

    public void Move() 
    { 
     if (MyLabel != null) 
     { 
      if (GoingForward) 
      { 
       MyLabel.Left += 5; 
      } 
      else 
      { 
       MyLabel.Left -= 5; 
       if (MyLabel.Left <= 0) 
       { 
        GoingForward = true; 
       } 
      } 
      if (MyLabel.Right >= MyLabel.Parent.Width) 
      { 
       GoingForward = false; 
      } 
     } 
    } 
    } 
} 
+0

これは機能しました。ありがとうございました!私は、彼らはそれを彼らは本のサンプルで行った方法を書いた理由しかし、私は思ったんだけど、ロジックを見ることができます。私は正当なエラッタを見つけることができません – nmonte

関連する問題