2012-07-04 82 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 
      }, 
     } 
    } 
相关问题