2012-07-04 81 views
0

我正試圖解決一本書的練習。 (沒有已發佈的答案)無法將現有的PictureBox對象分配給對象數組

我需要將我的窗體上存在的PictureBox對象引用到對象數組。 (我需要分配其中的四個)

我初始化數組並將變量賦值給它。然後,我調用數組中的每個項目中的方法,但PictureBox對象未分配。 (空例外)

我有點困惑,因爲我在網上公開了代碼片段,顯示我正確地做了這件事。

下面的代碼爲指針,請:

主類

public partial class Form1 : Form 
{ 
    Greyhound[] greyhoundArray = new Greyhound[4]; 

    public Form1() 
    { 
     greyhoundArray[0] = new Greyhound() { Location = 0, MyPictureBox = dog1, RaceTrackLenght = 100, StartingPosition = 0 }; 
     greyhoundArray[1] = new Greyhound() { Location = 0, MyPictureBox = dog2, RaceTrackLenght = 100, StartingPosition = 0 }; 
     greyhoundArray[2] = new Greyhound() { Location = 0, MyPictureBox = dog3, RaceTrackLenght = 100, StartingPosition = 0 }; 
     greyhoundArray[3] = new Greyhound() { Location = 0, MyPictureBox = dog4, RaceTrackLenght = 100, StartingPosition = 0 }; 

     InitializeComponent(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     foreach (Greyhound greyhound in greyhoundArray) 
     { 
      greyhound.Run(); 
     } 
    } 
} 

灰狗類

public class Greyhound 
{ 
    public int StartingPosition; 
    public int RaceTrackLenght; 
    public PictureBox MyPictureBox; 
    public int Location = 0; 
    public Random Randomiser; 

    public void Run() 
    { 
     // MessageBox.Show(MyPictureBox.Name + " was called"); 
     Randomiser = new Random(); 

     int distance = Randomiser.Next(0, 4); 

     Point p = MyPictureBox.Location; 
     p.X += distance; 
     MyPictureBox.Location = p; 
    } 

    public void TakeStartingPosition() 
    { } 
} 

而且我可以證實每條狗的PictureBox並在表格上存在:

片段來自Form1.Designer.cs

// 
// dog1 
// 
this.dog1.Image = ((System.Drawing.Image)(resources.GetObject("dog1.Image"))); 
this.dog1.Location = new System.Drawing.Point(17, 21); 
this.dog1.Name = "dog1"; 
this.dog1.Size = new System.Drawing.Size(71, 26); 
this.dog1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage; 
this.dog1.TabIndex = 2; 
this.dog1.TabStop = false; 
+1

它是InitializeComponent,初始化dog1,dog2等的值。所以先調用* first *,然後初始化你的數組。練習使用調試器,當你看着greyhoundArray時很容易看到。 –

回答

1

在GreyHound數組初始化之前調用InitializeComponent()

當你調用灰狗陣列的初始化,您還沒有叫裏面的InitializeComponent的this.dog1 = new PictureBox(),所以你複製一個空到每個灰狗實例

同樣的MyPictureBox財產,我認爲你有一個問題您的灰狗類中的Randomiser變量

+0

當然。我猜這些圖畫框在它們沒有活力之前是不存在的。 – Damo

+0

謝謝,你有沒有試過Run方法?這對於每個對象來說都是隨機的,還是一樣的? – Steve

+0

我現在正在努力,所有'狗'都以相同的速度移動。確實是一項進展中的工作。 – Damo

-1

您無法訪問窗體構造函數中的窗體控件。您需要在Form_Load事件處理程序中初始化您的灰狗的圖片框。

0

在聲明數組中的任何內容之前,您應該初始化表單的控件,也就是說,因爲您的數組正在引用表單中的項目。

Greyhound[] greyhoundArray; 

    public Form1() 
    { 
     InitializeComponent(); 
     greyhoundArray = new Greyhound[] { 
      new Greyhound() { 
      Location = 0, 
      MyPictureBox = dog1, 
      RaceTrackLenght = 100, 
      StartingPosition = 0 
      }, 
      new Greyhound() { 
      Location = 0, 
      MyPictureBox = dog2, 
      RaceTrackLenght = 100, 
      StartingPosition = 0 
      }, 
      new Greyhound() { 
      Location = 0, 
      MyPictureBox = dog3, 
      RaceTrackLenght = 100, 
      StartingPosition = 0 
      }, 
      new Greyhound() { 
      Location = 0, 
      MyPictureBox = dog4, 
      RaceTrackLenght = 100, 
      StartingPosition = 0 
      }, 
     } 
    } 
相關問題