Solved

Dynamically creating Picture Box Control

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now