-1
private void radar_Tick(object sender, EventArgs e)
{
    for (int i = 0; i < 20; i++)
    {
        x[i] = Readi(0x5C850C + (0x5A4 * i));
        y[i] = Readi(0x5C8510 + (0x5A4 * i));
        players[i].Location = new Point(x[i], y[i]);
    }
}

and yeah, before that:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    int []x=new int[32];int []y=new int[32];
    PictureBox[] players=new PictureBox[32];    
    ...  
}
private void Form1_Load(object sender, EventArgs e)
    {
        int i = 0;
        foreach (var ctrl in this.Controls)
        {
            if (ctrl is PictureBox)
            {
                var myPicturebBox = (PictureBox)ctrl;
                players[i] = myPicturebBox;
                this.Controls.Add(players[i]);
                i++;

            }
        }
        radar.Interval = 100;
        radar.Start();
    }

What I keep getting is "Object reference not set to an instance of an object". I don't want to make new object on every timer tick, I just want to change i-th picturebox's location. Any ideas? Nulls are not x and y,null is pictebox array.(I tried adding players[i]=new PictureBox(); and it worked.That's how I know)

Turus
  • 11
  • 5

1 Answers1

2

If you already have 32 PictureBox objects and you want to change their Locations, you need to make each of the players array members reference a PictureBox.

int i = 0;
foreach (var ctrl in this.Controls)
{
    if (ctrl is PictureBox)
    {
        var myPicturebBox = (PictureBox)ctrl;
        players[i] = myPictureBox;
        i++;
    }
}

Now your array members reference all of the PictureBox objects in your Form.

Edit

Since your PictureBox objects are all created programmatically, you have to instatiate them before you can use them, like this:

PictureBox myPictureBox = new PictureBox();

Now you need to add the PictureBox to the Controls collection of the Form:

this.Controls.Add(myPictureBox)

Then you can use the previous code, do this for all of the PictureBoxes using a loop.

Youssef Lourayad
  • 337
  • 3
  • 14