Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Dynamic labels on a form

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
bryanford
Asked:
bryanford
  • 4
  • 3
1 Solution
 
omegaomegaDeveloperCommented:
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
 
bryanfordAuthor Commented:
I figured out an easier way myself. I used a TableLayoutPanel control from the toolbox and set autoscroll to true. Works marvelous.
0
 
omegaomegaDeveloperCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
bryanfordAuthor Commented:
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
 
omegaomegaDeveloperCommented:
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
 
bryanfordAuthor Commented:
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
 
omegaomegaDeveloperCommented:
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
 
RomModCommented:
Question closed - 250 points refunded.

Best regards,
RomMod
Experts Exchange
Community Support Moderator
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now