Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning Visual C# 2005
This is the forum to discuss the Wrox book Beginning Visual C# 2005 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780764578472
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 26th, 2008, 12:20 PM
Registered User
 
Join Date: Nov 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

 
Old November 26th, 2008, 01:00 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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
================================================== =========
 
Old November 27th, 2008, 04:26 AM
Registered User
 
Join Date: Nov 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Vertical Labels bfoley CSS Cascading Style Sheets 2 November 26th, 2007 02:06 PM
Labels on form sweet4511 VB How-To 2 July 20th, 2005 11:46 AM
Labels in Access taraj Access 2 May 16th, 2005 01:35 PM
Labels slgknjn Beginning VB 6 9 February 25th, 2005 04:12 PM
Labels in DataLists shmacgregor ASP.NET 1.0 and 1.1 Basics 4 April 2nd, 2004 03:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.