?
Solved

Dynamically creating Picture Box Control

Posted on 2012-03-27
4
Medium Priority
?
361 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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 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