Windows Develop Bookmark and Share   
 index > Windows Forms General > dynamic pictureBox creation
 

dynamic pictureBox creation

Hi all,

I have a form that contains one button and one textbox, the textbox will take a number that will be passed to another form. I have done this successfully.
The thing is that I need the second form to create a number of pictureboxes = the number received from the first form.

Any ideas?
SarahMalik  Monday, March 12, 2007 6:10 PM

Hi,

the following code will create a number of PictureBox controls and add them to your Form:

int receivedNumber = 5;
for (int pictureIndex = 0; pictureIndex < receivedNumber; pictureIndex++)
{
PictureBox box = new PictureBox();
// set box.Location and box.Size here
Controls.Add(box);
}

Andrej

Andrej Tozon  Monday, March 12, 2007 7:54 PM

Hi Andrej,

I think your code is in VB.NET, isn't it?

actually I'm using C++.NET and I've tried a similar codein the form-Load action, but it seems that I cannot declare a new control (pictureBox - in my case ) inside this form_load.

int receivedNumber = 5;
for (int
pictureIndex = 0; pictureIndex < receivedNumber; pictureIndex++)
{

System::Windows::Forms::PictureBox^ pictureBox1 = (gcnew System::Windows::Forms::PictureBox());

// set pictureBox1->Location and pictureBox1->Size here
}

SarahMalik  Monday, March 12, 2007 9:18 PM

Hi,

the following code will create a number of PictureBox controls and add them to your Form:

int receivedNumber = 5;
for (int pictureIndex = 0; pictureIndex < receivedNumber; pictureIndex++)
{
PictureBox box = new PictureBox();
// set box.Location and box.Size here
Controls.Add(box);
}

Andrej

Andrej Tozon  Monday, March 12, 2007 7:54 PM

Hi Andrej,

I think your code is in VB.NET, isn't it?

actually I'm using C++.NET and I've tried a similar codein the form-Load action, but it seems that I cannot declare a new control (pictureBox - in my case ) inside this form_load.

int receivedNumber = 5;
for (int
pictureIndex = 0; pictureIndex < receivedNumber; pictureIndex++)
{

System::Windows::Forms::PictureBox^ pictureBox1 = (gcnew System::Windows::Forms::PictureBox());

// set pictureBox1->Location and pictureBox1->Size here
}

SarahMalik  Monday, March 12, 2007 9:18 PM

I managed to do it.

thanks alot,

SarahMalik  Monday, March 12, 2007 9:56 PM

SarahMalik wrote:
I think your code is in VB.NET, isn't it?

It's C#.

Glad you got it working...

Andrej

Andrej Tozon  Monday, March 12, 2007 10:05 PM

You can use google to search for other answers

Custom Search

More Threads

• Delete selected items from listbox
• Bug on VS.NET 2003 Start Page?
• How to execute a command line parameters from visual C# forms
• Textbox control maxlength 70, yet can paste strings longer than 70 chars?
• AxDSOFramer.AxFramerControl
• MonthCalendar weird behaviour
• Fade in/Fade out
• Close App on Shutdown
• Question regarding exam 70-505
• windows click event