Solved

Dynamically creating Picture Box Control

Posted on 2012-03-27
4
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…

730 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