p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning Visual C# 2005 (http://p2p.wrox.com/forumdisplay.php?f=222)
-   -   dynmanic labels (http://p2p.wrox.com/showthread.php?t=71615)

icebot November 26th, 2008 12:20 PM

dynmanic labels
 
hi,
I'm new here and because I didn't found the awnser in this book and not in course, will I ask it here, I hope I'll get a solution on my problem.

this is the situation:

I wanna create a program that is able to create dynamically by counting results from a SQL-query(that phase is easy I know how to do it), but thereafter, I want that the solution automatically creates the labels,
like :

label1 label2 label3
label4 label5 label6

and that on a form :s

I hope it is possible :s


dparsons November 26th, 2008 01:00 PM

Actually creating the labels isn't really that difficult. Assume that you have a placeholder on your form named phControls your code would look something like this:

(Assume dt is a DataTable object)
for(int i = 0; i < dt.Rows.Count();i++)
{
   Label lbl = new Label();
   lbl.ID = "lbl" + Convert.ToString(i);
   lbl.Text = Convert.ToString(dt.Rows[i]["SomeColumn"]);
   phControls.Controls.Add(lbl);
}

This is pretty simple, however, if you run the code as is you will only see the last label added to the placeholder because each label in the code above would be placed in the exact same position within the placeholder, effectively stacking subsequent labels on top of one another.

To over come this assign values to the Top and Left properties of the label so:
lbl.Top = 10;
lbl.Left = 15;

Obviously you will need to change these values on each iteration of the loop but this should give you a good starting point.

hth.
-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========

icebot November 27th, 2008 04:26 AM

thx for the post,
I will try it as soon as I can :)



All times are GMT -4. The time now is 12:21 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.