Dynamic Object Naming

  • Thread starter Thread starter TheLostLeaf
  • Start date Start date
T

TheLostLeaf

How do I change this :

this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.pictureBox4 = new System.Windows.Forms.PictureBox();
this.pictureBox5 = new System.Windows.Forms.PictureBox();

To This:

for (int i = 1; i < 6; i++)
{
this.pictureBox+(i) = new System.Windows.Forms.PictureBox();
}

Do i need to put it into an array or something? Thanks for any help ...
 
TheLostLeaf said:
How do I change this :

this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.pictureBox4 = new System.Windows.Forms.PictureBox();
this.pictureBox5 = new System.Windows.Forms.PictureBox();

To This:

for (int i = 1; i < 6; i++)
{
this.pictureBox+(i) = new System.Windows.Forms.PictureBox();
}

Do i need to put it into an array or something? Thanks for any help ...

Hi,

An array would be the way to go:

/// At the top somewhere
private PictureBox[] pictureBox = new PictureBox[6];
///

/// In your code somewhere
for ( int i = 0; i < pictureBox.Length; i++ )
this.pictureBox = new PictureBox();
///

Note that arrays are zero-based.
 
Hi,
you are right, just put your picture boxes in an array:

const int numberOfPictureBoxes = 5;
PictureBox[] pictureBoxes = new PictureBox[numberOfPictureBoxes];
for(int i=0; i<numberOfPictureBoxes; ++i)
{
pictureBoxes = new PictureBox();
}

HTH
Mark.
 
That got me on the right track.

/// I put this in the class
private System.Windows.Forms.PictureBox[] pictureBox;

///i put this in the funciton
pictureBox = new System.Windows.Forms.PictureBox[10];

for ( int i = 0; i < pictureBox.Length; i++ )
{
this.pictureBox = new
System.Windows.Forms.PictureBox();

((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
}
Works perfect,,, Thanks !


Tom said:
TheLostLeaf said:
How do I change this :

this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.pictureBox4 = new System.Windows.Forms.PictureBox();
this.pictureBox5 = new System.Windows.Forms.PictureBox();

To This:

for (int i = 1; i < 6; i++)
{
this.pictureBox+(i) = new System.Windows.Forms.PictureBox();
}

Do i need to put it into an array or something? Thanks for any help ...

Hi,

An array would be the way to go:

/// At the top somewhere
private PictureBox[] pictureBox = new PictureBox[6];
///

/// In your code somewhere
for ( int i = 0; i < pictureBox.Length; i++ )
this.pictureBox = new PictureBox();
///

Note that arrays are zero-based.

--
Hope this helps,
Tom Spink

Google first, ask later.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top