How to determine if System.Windows.Forms.Label with AutoEllipsis really displays ellipsis?

I have a Windows Forms application in which I display some client data in a shortcut. I set label.AutoEllipsis = true.
If the text is longer than the label, it looks like this:

Some Text Some longe... // label.Text is actually "Some longer Text" // Full text is displayed in a tooltip 

what I want.

But now I want to know if the label uses the AutoEllipsis function at run time. How to do it?

Decision

Thanks max. Now I managed to create a control that tries to put all the text on one line. If anyone is interested, here is the code:

 Public Class AutosizeLabel Inherits System.Windows.Forms.Label Public Overrides Property Text() As String Get Return MyBase.Text End Get Set(ByVal value As String) MyBase.Text = value ResetFontToDefault() CheckFontsizeToBig() End Set End Property Public Overrides Property Font() As System.Drawing.Font Get Return MyBase.Font End Get Set(ByVal value As System.Drawing.Font) MyBase.Font = value currentFont = value CheckFontsizeToBig() End Set End Property Private currentFont As Font = Me.Font Private Sub CheckFontsizeToBig() If Me.PreferredWidth > Me.Width AndAlso Me.Font.SizeInPoints > 0.25! Then MyBase.Font = New Font(currentFont.FontFamily, Me.Font.SizeInPoints - 0.25!, currentFont.Style, currentFont.Unit) CheckFontsizeToBig() End If End Sub Private Sub ResetFontToDefault() MyBase.Font = currentFont End Sub End Class 

Fine tuning may be required (make a step size and a minimum value that can be set using the visible properties of the designer), but at the moment it works very well.

+4
source share
2 answers
 private static bool IsShowingEllipsis(Label label) { return label.PreferredWidth > label.Width; } 
+13
source

In fact, your Lable can be multi-line. In this case, label.PreferredWidth will not help. But you can use:

  internal static bool IsElipsisShown(this Label @this) { Size sz = TextRenderer.MeasureText(@this.Text, @this.Font, @this.Size, TextFormatFlags.WordBreak); return (sz.Width > @this.Size.Width || sz.Height > @this.Size.Height); } 
+1
source

Source: https://habr.com/ru/post/1312422/


All Articles