0

我正在尝试从书中解决一个练习。(没有公布的答案)

我需要将表单上存在的 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()
    { }
}

我还可以确认表格上确实存在每只狗的图片框:

来自 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;
4

3 回答 3

1

InitializeComponent()在初始化 GreyHound 数组之前调用。

当你调用 Greyhound 数组的初始化时,你还没有调用this.dog1 = new PictureBox()里面的 InitializeComponent,所以你将一个 null 复制到MyPictureBox每个 Greyhound 实例的属性中

另外,我认为您的 Greyhound 课程中的 Randomiser 变量有问题

于 2012-07-04T21:30:32.163 回答
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
          },
       }
    }
于 2012-07-04T21:34:40.393 回答
-1

您不能在表单的构造函数中访问表单控件。您需要在 Form_Load 事件处理程序中初始化您的灰狗的图片框。

于 2012-07-04T21:27:37.533 回答