1

I have a form with a status strip, which contains a progress bar an a label. Both of these are used to show the user the status/progress of several background workers.

My problem is that sometimes the label is longer than the form is wide (it contains Parameter names that vary quite widely in length). The form has a constant width and is not re-sizable by the user. When this issue occurs the label just appears as blank, I would instead like to cut the label to the length of the form and concatenate "..." on the end.

Can anyone give me some advise on where to start with this? i have tried Google and SO searches and have been unable to come up with anything similar. I essentially need to find the length of the string as it will display on the form, but I don't know where to start with that.

Pezzzz
  • 738
  • 4
  • 14
  • 33
  • Do you know in what momment you label is bind your label with info ? if you able then you can check length after apply into your label.text property and only show some words and add .. to end. – danywalls Apr 24 '13 at 10:46
  • @danywalls. Ok, I do know where I set the label.text to the string. I can check .width here and get a value in pixels and work with that? – Pezzzz Apr 24 '13 at 10:52
  • 1
    Yes check this question http://stackoverflow.com/questions/388937/determine-label-size-based-upon-amount-of-text-and-font-size-in-winforms-c this example show how Determine Label Size based upon amount of text and font size :) – danywalls Apr 24 '13 at 10:55
  • are you think use autosize property ? http://msdn.microsoft.com/es-es/library/system.windows.forms.label.autosize.aspx – danywalls Apr 24 '13 at 11:02

2 Answers2

2

First thing to do is to change the StatusStrip.LayoutStyle from Table to Flow. Which will prevent the label from disappearing. Next, you still want the user to have a chance to read the full text of the label even though it is truncated. Set the StatusStrip.ShowItemToolTips property to True and the label's AutoToolTip to True.

Getting the label's text to not overlap the grip is an uglier problem to fix but one you don't have since you made your form un-resizable. Set the form's SizeGripStyle property to Hide.

This will fix your problem, no code required.

Pezzzz
  • 738
  • 4
  • 14
  • 33
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • +1 Great answer. I suggest another nice possibility: what about resizing the text font (with a readable limit) to fit the space given (just like Apple apps usually do on his forms) – SysDragon Apr 24 '13 at 11:17
  • +1, it is almost there however the status strip now puts the progress bar above the label (making the status strip twice as tall). Is there a way to avoid this? – Pezzzz Apr 24 '13 at 11:33
  • @SysDragon nice idea but the font has a consistent size throughout the form, and the information in the label becomes less 'useful' to the right. Most of the strings fit it is only those with a long Parameter name as the end. And the user can't make any further interactions with the app specific to what the label is displaying. – Pezzzz Apr 24 '13 at 11:41
  • I didn't count on the ProgressBar. Back to Table, set the label's Spring property to True. Make sure it is the last item, progress bar should be first. – Hans Passant Apr 24 '13 at 11:49
  • I have followed the last couple of steps and it works. Thanks – Pezzzz Apr 24 '13 at 13:07
0

You can try something like this:

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    If Label1.Text.Length > iMaxLblLenght Then
        Label1.Text = Label1.Text.Substring(0, iMaxLblLenght) & "..."
    End If
End Sub
SysDragon
  • 9,692
  • 15
  • 60
  • 89