我有一个 Windows 窗体应用程序,在其中在标签中显示一些客户端数据。
我已设置 label.AutoEllipsis = true。
如果文本比标签长,则如下所示:
Some Text
Some longe... // label.Text is actually "Some longer Text"
// Full text is displayed in a tooltip
这就是我想要的。
但现在我想知道标签是否在运行时使用自动省略功能。 我该如何实现这一目标?
解决方案
感谢最大。现在我能够创建一个控件,尝试将整个文本放在一行中。如果有人感兴趣,这里是代码:
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
可能需要一些微调(使步长和最小值可通过设计器可见的属性进行配置),但目前效果很好。
最佳答案
private static bool IsShowingEllipsis(Label label)
{
return label.PreferredWidth > label.Width;
}
关于c# - 如何检测带有自动省略号的 System.Windows.Forms.Label 是否确实显示省略号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3013247/