Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect if a System.Windows.Forms.Label with AutoEllipsis is actually displaying ellipsis?

I have a Windows Forms Application where I display some client data in a Label. I have 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

which is what I want.

But now I want to know if the label makes use of the AutoEllipsis feature at runtime. How do I achive that?

Solution

Thanks to max. Now I was able to create a control that try to fit the whole text in one line. If someone is interested, here's 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

Could need some fine tuning (make the step size and the minimum value configurable with designer visible Properties) but it works pretty well for the moment.

like image 618
Jürgen Steinblock Avatar asked Jun 10 '10 09:06

Jürgen Steinblock


People also ask

What is AutoEllipsis?

Remarks. Set AutoEllipsis to true to display text that extends beyond the width of the Label in a tooltip when the user passes over the control with the mouse. If AutoSize is true , the label will grow to fit the text and an ellipsis will not appear.

Where do I find labels in Windows form?

On the tab "Properties" just click on the arrow to show all controls and click on the label you want, this will automatically select the label on your form..

What is Label in Windows form?

In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. The Label is a class and it is defined under System.Windows.Forms namespace.


1 Answers

private static bool IsShowingEllipsis(Label label)
{
    return label.PreferredWidth > label.Width;
}
like image 181
max Avatar answered Nov 15 '22 08:11

max