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 :

frm1.SetImage(Properties.Resources.b1);
                Hide();

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.

Recommended Answers

All 6 Replies

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()
        {
            InitializeComponent();
        }

        //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);
            newForm.Show();
        }

        //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()
        {
            InitializeComponent();
        }

        //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
            InitializeComponent();

            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.

commented: helpfull +6

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.

Works perfect now :)
Thanks!

So why don't you consider this as solved? It's just a click away...

oh, I am new to this board system.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.