Show "..." at the end of a label if it is too wide - winforms

I know it is possible to set MaximumSize to prevent a label from resizing off of a form, but is it possible to also clip the text if necessary?
For example, if you pass the sentence Hello, my name is Dave. to a label, making the width greater than the maximum, I'd like it to show Hello, my name is.... Thanks.

Use the AutoEllipsis property and don't forget to set AutoSize to False
MSDN says:
Gets or sets a value indicating whether the ellipsis character (...)
appears at the right edge of the Label, denoting that the Label text
extends beyond the specified length of the Label.

Related

gtk label text center and wrap

I have been experiencing some really weird problems with gtk_label text positioning.
I have a gtk_label positioned on a fixed container, the label has been set to:
gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
However when a single word is present in the label, instead of getting centered it gets left aligned.
If I unset gtk_label_set_line_wrap(GTK_LABEL(label), TRUE) to FALSE the word then appears in the center of the label, but I lose wrapping.
How should this be fixed?
From the documentation (emphasis mine):
gtk_label_set_justify ()
Sets the alignment of the lines in the text of the label relative to each other. GTK_JUSTIFY_LEFT is the default value when the widget is first created with gtk_label_new(). If you instead want to set the alignment of the label as a whole, use gtk_misc_set_alignment() instead. gtk_label_set_justify() has no effect on labels containing only a single line.
As suggested in the above quote, you may wish to add a call to the gtk_misc_set_alignment() function, with its xalign parameter set to 0.5, before calling gtk_label_set_justify().

Telerik RadMaskedTextBox focus Mask cursor spacing

I keep running into this increasingly annoying nuance when using a RadMaskedTextBox wherein once the field gains focus, the cursor doesn't seem to position itself at the beginning of the field. Instead I get something like this;
Which would come from for example, the code below. Notice in the example the cursor starts 7 spaces from the beginning of the field. Which is exactly the number of #'s I have set in my Mask to allow numeric input of 7 characters as shown below.
<telerik:RadMaskedTextBox
Value="{Binding Path=Parameters.Blah, Mode=TwoWay}"
MaskType="Standard"
Placeholder=""
Mask="#######"/>
How can I get the cursor back to the beginning of the field when it receives focus while keeping my numeric input length? I have the suspicion I'm missing an inane detail here. Thanks in advance.
I'm not sure what you're asking for... but if you just want to get the cursor in the beginning of the radmaskedtextbox on focus (once you are focused, you can click wherever in the textbox to move the cursor) you should use this property:
SelectionOnFocus="CaretToBeginning"

WPF TextBlock Cutoff

Hi Guyz I have a WPF TextBlock of fixed width say 100 , If the string doesnt fit in the width the last character is being cutoff always as all the characters are of not the same size. I dont want to cut the character instead I want to skip the text from there and just display the text with no character cutoff.
You have a couple of options to control wrapping and cutting of text:
TextWrapping can be used to make the text flow to the next line
TextTrimming can be used to decide how to cut text that doesn't fit
TextTrimming=None (the default) will mean that text which doesn't fit will be hidden, but it may cut down the middle of a character, which sounds like the problem you describe.
TextTrimming=WordEllipsis or TextTrimming=CharacterEllipsis will avoid showing half a character, but will append "..." to the end of the text. That will probably look better to users.
If you want to cut off the extra characters without adding the ellipsis, you'd have to use the technique Ed S. described
I suppose that I don't really understand your use case here. My first suggestion would be to simply dynamically size your TextBlock. If that's not possible then you wil have to get the width of the string and manipulate it yourself before you set it in the TextBlock (or use a fixed width font assuming that you can and you know the max length of the string).
If you need to measure the width of the string before it is displayed you can use the FormattedText class to do so.

Need solution for Text trimming Problem

We are facing problem regarding Text trimming in TextBlock.
We have a Text "My very long label goes here" and we have to show it in following format:
[...g label goes here]
i.e right aligned and text should be trimmed from left (character ellipsis at left side)
We have used FlowDirection of TextBlock, ellipsis is correctly shown on the left side, but text is trimmed from the right side.
FlowDirection = "LeftToRight:"
[My very long lab...]
FlowDirection = "RightToLeft:"
[...My very long lab]
But we need:
[...g label goes here]
Please provide suggestions on this.
You don't mention how the TextBlock gets its string, but if its from a binding, have you considered using a value converter to just trim the string?
I guess this is also only applicable if your requirement is X number of characters before trimming, not if your requirement is Y number of pixels..

Resizing Labels

I have a chart in WPF with a lot of labels. The text on these labels is dynamically loaded and subject to change. If I set the width just to auto, then these labels may overlap, which makes the text unreadable.
The chart support multiple sizes, so if it gets larger, then the bars are re sized and there is more space for text. Now I want to adjust the text to the space which is available. If it gets too small, I don't want to display the label anymore (a tooltip is available, so the user still gets the required information). Consider the string "Case 1, blah blah", there is probably not enough space to display the whole string, but just the first word. In this case I want the string to be "Case 1..", with .. indicating that there is some more information in the tooltip.
I can determine the length available for the string. But how can I determine the space a single letter will take? Of course I could also just re size the label, but then it would just cut off the string anywhere which is probably not helpful for the user (and looks ugly).
Any ideas?
If you can use TextBlocks instead of labels then they have a TextTrimming property which will do this for you to either the nearest character or the nearest word.
While you seem happy with the TextTrimming property, I'll edit this to add that the TextBox control has a GetRectFromCharacterIndex method that would allow you to find out the size on screen of one or more characters as long as the font settings matched your label. This might be useful if you wanted to trim at specific places in the label rather than the nearest character / word.
Not an expert in WPF, but I would think that you'll need to do this in code rather than XAML.
Start by obtaining the actual pixel width of the space available for the text.
Then look at the character set, dot pitch etc. utilised on the XAML front end and from there calculate the pixel width required per character.
You could also look at changing the character sizes as well as reducing the label length.

Resources