Solved

Dynamic labels on a form

Posted on 2006-06-14
9
252 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

9 Experts available now in Live!

Get 1:1 Help Now