Solved

Dynamically creating Picture Box Control

Posted on 2012-03-27
4
334 Views
Last Modified: 2012-03-27
Hi

I want to dynamically add several [Picture Box] controls on my windows form. The number dynamically created will depend on what a SELECT statement returns of rows count at run time from the database.

What's the code segment that creates a Picture Box dynamically?
0
Comment
Question by:alfardan
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37774992
http://www.daniweb.com/software-development/csharp/threads/283982/dynamically-adding-picturebox-with-loop

private void button2_Click(object sender, EventArgs e)
{
List<Image> pictures = new List<Image>(GetImagesFromDB());
List<PictureBox> lst = new List<PictureBox>();
for (int i = 0; i < pictures.Count; i++)
{
PictureBox pb = new PictureBox()
{
Size = new Size(75, 75),
Location = new Point(5, (i * 80) + 5),
BorderStyle = BorderStyle.Fixed3D,
Image=pictures[i]
};
lst.Add(pb);
this.Controls.Add(pb);
pb.BringToFront();
pb.Click += new EventHandler(pb_Click);
}
}

Open in new window


above is the code to add picture boxes, i am sure you can always run a sqlcommand and use executeadapter method and get the data in datatable/dataset.

The count of loop can be then set to count of rows in datatable returned.
0
 

Author Comment

by:alfardan
ID: 37775112
Thanks BuggyCoder

Event though I was looking for VB code, but I managed to convert it to VB using a C#-to-VB convertor tool

Anyway, what shall this (GetImagesFromDB())) return exactly?
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37775124
this will contain the code to fetch data from database, probably the number of images and other image detail if any.

you can use SQLCommand and SQLDataAdapter/SqlDataReader for that purpose....
0
 

Author Comment

by:alfardan
ID: 37775165
Got it, it works perfectly now, thank you
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question