views:

101

answers:

1

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.

+5  A: 
private static bool IsShowingEllipsis(Label label)
{
    return label.PreferredWidth > label.Width;
}
max
Phil
Thanks, I updated the Question with a control that autofits the text to fit in the label. @Phil: good point, too!
SchlaWiener