I created 100 pictures boxes for my game with this code :

for (int p = 0; p <= 9; p++)
                for (int o = 0; o <= 9; o++)
                    picsBoard[p, o] = new PictureBox();
                    picsBoard[p, o].Name = "emptyPicBox" + p.ToString() + "." + o.ToString();
                    picsBoard[p, o].Location = new Point(40 + locCountX, 38 + locCountY);
                    picsBoard[p, o].Size = new Size(32, 36);
                    picsBoard[p, o].BackColor = System.Drawing.Color.Transparent;
                    picsBoard[p, o].BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
                    picsBoard[p, o].Visible = true;
                    this.Controls.Add(picsBoard[p, o]);
                    locCountX += 43;
                    picsBoard[p, o].BringToFront();
                locCountY += 43;
                locCountX = 0;

then, I want to change the Image in every pB from another form, thats why I wrote this code :

public void SetImage(Image img)
            picsBoard[this.i, this.j].Image = img;

and im using it from Form2 :


but when I trying this thing all the pictureBoxes I created simply becoming 'null' again :\

and it gives the error :

Object reference not set to an instance of an object.
8 Years
Discussion Span
Last Post by danieldot

I think you have hit a very common problem amongst new coders. You are trying to access members on your instance of Form1 via its name instead of a handle. Unless you are dealing with static classes/members you cannot access them via their name alone.
When your application runs, in the program file you should see something like Application.Run(new Form1()); . This creates an instance of your form. Inside your form you are probably calling something like Form2 newForm = new Form2(); .
At this point you have created an instance of Form2 and have stored a pointer to that instance in the variable newForm. newForm acts as a handle which allows you to interact with your instance of Form2.

You need to have a similar handle inside Form2 that you can use to access your instance of Form1. You can do this by passing a reference to the Form2 constructor and storing it within the class (code to follow shortly).


Heres a quick example of passing a handle to a new form:

public partial class Form1 : Form
        public Form1()

        //button handler to create new form 2
        private void btnForm2_Click(object sender, EventArgs e)
            //pass a reference to this instance of Form1
            Form2 newForm = new Form2(this);

        //public method to change textbox text value
        public void WriteToTextbox(string inputText)
            this.txtForm1Text.Text = inputText;

public partial class Form2 : Form

        //default constructor
        public Form2()

        //overloaded constructor with handle to Form1
        public Form2(Form1 frm1Handle)
            //its important to include this in your custom constructors
            //it calls the designer generated code to create the controls on your form

            HandleToForm1 = frm1Handle;

        //local variable to store handle to Form1
        private Form1 HandleToForm1;

        //when button on form 2 is clicked the text is written to Form1 via the handle
        private void btnWriteToForm1_Click(object sender, EventArgs e)
            HandleToForm1.WriteToTextbox("This came from Form2");

You can see the attached image for to see the forms.
When you click on the button on Form1 it creates a new instance of Form2 and (using a custom constructor) passes it a reference to itself.
Form2 stores that reference and, when the user clicks a button on Form2, it uses the reference to access the members of the original instance of Form1.

Votes + Comments
Attachments FormHandle.JPG 18.64 KB

Comment on the side: never use o as an index variable! Can be confused with 0 (tell me: is this the letter o or the digit o?) I used to program in FORTRAN IV. If you used a letter like I, J, K... it was an integer. I think these days, that is why we still often use i or j as loop counters.

Edited by ddanbe: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.