Solved

Dynamic labels on a form

Posted on 2006-06-14
9
254 Views
Last Modified: 2010-04-23
Hello,

I have a form with 1 column of about 6 or 7 labels.

These label's text is generated from a mdb database

Sometimes, the text in the database is really long and I need a way to make the label multilined, shift everything below the label down and add a scrollbar if needed.

How would i go about this?

Bryan
0
Comment
Question by:bryanford
  • 4
  • 3
9 Comments
 
LVL 12

Expert Comment

by:omegaomega
ID: 16909721
Hello, Bryan,

I think that it is a bit difficult to determine the vertical size that will be required for the labels.  One of the overloads of the Graphics "MeasureString" method returns a line count.  I haven't done this, but I imagine that you could use this in the TextChanged event multiplied by the individual line height to set the height of the label's ClientSize property.  Then set the next label's position based on the position and height of this label (with an allowance for a vertical gap).  Perhaps you can use the PreferredHeight property to get the individual line height, and the GetNextControl method to determine which is the following label that needs to be moved.

Then in the Label's LocationChanged event, also set the next label's position, so that the position adjustment is propagated through all of the Form's labels.  (I'm assuming here that you are assigning the same event handler to each of the labels involved.)

Finally, just make sure that the Form's AutoScroll property is set to True, so that scroll bars will appear when required.

But, if all that seems too difficult, instead of labels you might consider using multiline textboxes (with vertical scroll bars, and ReadOnly property set to True.)

Cheers,
Randy
0
 
LVL 1

Author Comment

by:bryanford
ID: 16910251
I figured out an easier way myself. I used a TableLayoutPanel control from the toolbox and set autoscroll to true. Works marvelous.
0
 
LVL 12

Expert Comment

by:omegaomega
ID: 16910522
Hello, Bryan,

That's great that you found an easy solution.  

I guess this is a new control available in v2005.  (I'm still using v2003, so I cna't try it out.)  It looks very useful, but I can't see immediately how it handles the problem of resizing to accommodate all of the required text.  

Can you please give us a brief description of the technique that you used.  Thanks.

Cheers,
Randy
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:bryanford
ID: 16910539
Create a new form and in the tool box there is an item called TableLayoutPanel. Drag this in and modify its properties to specify how many rows and columns you want. Then simply drag some labels into each column, format as needed, then bind to a datasource. Thats all there is to it really. you can scroll the form if it exceeds the form size and best of all, if the text is really long, it will shift everything below it down to the margin you specify
0
 
LVL 12

Expert Comment

by:omegaomega
ID: 16910694
Hi, Bryan,

Thanks for the explanation.  So does this mean that in v2005 the Labels can be made to resize vertically instead of just horizontally?  That would be great, and I'll look forward to using it.

Cheers,
Randy
0
 
LVL 1

Author Comment

by:bryanford
ID: 16916806
if you set the maximum horizontal size in the properties, if it exceeds that it will automatically make it a multiline. is that what you mean?
0
 
LVL 12

Expert Comment

by:omegaomega
ID: 16918007
Hello, RomMod,

In my opinion, Bryan HAS responded diligently and adequately provided his solution.

Cheers,
Randy

Hello, Bryan,

Yes, MaximumSize appears to provide the answer.  I see that there are a lot of new properties available in v2005, and will hope that I can begin to apply some of them in my next project.

Cheers,
Randy
 
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 16970541
Question closed - 250 points refunded.

Best regards,
RomMod
Experts Exchange
Community Support Moderator
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 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