无法将现有PictureBox对象分配给Object Array

时间:2012-07-04 21:23:47

标签: c# winforms

我试图用书来解决练习。 (没有公布的答案)

我需要将表单上存在的PictureBox对象引用到Object Array。 (我需要真正指定其中的四个)

我初始化数组并为其分配变量。然后我在数组中的每个项目中调用一个方法,但是没有分配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();
        }
    }
}

Greyhound Class

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;

3 个答案:

答案 0 :(得分:1)

在初始化GreyHound数组之前将调用放到InitializeComponent()

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

另外,我认为您的Greyhound类中的Randomiser变量存在问题

答案 1 :(得分: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
          },
       }
    }

答案 2 :(得分:-1)

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