Hi,I want to create a picturebox at runtime.i mean when i click a button then,the picturebox is created.
i have tried this code, but it doesnt work.

PictureBox^ pct;

                                 pct=gcnew PictureBox;
				
				pct->Location = Point(240,210);

				pct->Size.Height= 30;

				pct->Size.Width= 30;

				pct->BackColor.Aquamarine; 
				 Controls->Add(pct);

				 pct->Visible=true;

i am using visual c++ 2008.

after i create a picturebox,i want to create an array of pictureboxes.can anybody please tell me how to create a picturebox and its array.

Thanks

Try using this code instead:

PictureBox ^pBox = gcnew PictureBox();

pBox->Parent = this; // 'this' is pointing to our form (the parent)
pBox->Location = Point( 240, 210 );
pBox->Size::set( Drawing::Size(30, 30) );
pBox->BackColor::set( Color::Aquamarine );

And as for making an array of PictureBoxs, this code will make an array that can hold 100 of them.

// Create an array with 100 picture boxes
array<PictureBox^> ^pBoxes = gcnew array<PictureBox^>( 100 );

Then you simply have to assign each element, for example:

pBoxes[0] = pBox;

Hope this helps.

commented: thanks a lot +1

thanks a lot,it works now.can you please explain the array a bit,i dont understand it.
Thanks again!

An array is simply a list of objects (in this case, PictureBox is the object).

The code:

// Create an array with 100 picture boxes
array<PictureBox^> ^pBoxes = gcnew array<PictureBox^>( 100 );

makes an array which you can assign 100 PictureBoxs too.


So you could then do...

pBoxes[0] = pBox1;
pBoxes[1] = pBox2;
pBoxes[2] = pBox3;
pBoxes[3] = pBox4;
// ...

If you don't understand that, try researching arrays.

An array is simply a list of objects (in this case, PictureBox is the object).

The code:

// Create an array with 100 picture boxes
array<PictureBox^> ^pBoxes = gcnew array<PictureBox^>( 100 );

makes an array which you can assign 100 PictureBoxs too.


So you could then do...

pBoxes[0] = pBox1;
pBoxes[1] = pBox2;
pBoxes[2] = pBox3;
pBoxes[3] = pBox4;
// ...

If you don't understand that, try researching arrays.

Thanks a lot.... ;p

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.