Solved

Dynamically creating Picture Box Control

Posted on 2012-03-27
4
339 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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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