我有一个Windows窗体应用程序,我在标签中显示一些客户端数据。 我已经设置label.AutoEllipsis = true。
如果文本大于标签长,它看起来像这样:如何检测带有AutoEllipsis的System.Windows.Forms.Label是否实际显示省略号?
Some Text
Some longe... // label.Text is actually "Some longer Text"
// Full text is displayed in a tooltip
这就是我想要的。
但现在我想知道标签是否在运行时使用AutoEllipsis功能。 如何存档?
解决方案
感谢最大。现在我可以创建一个控件,尝试将整个文本放在一行中。如果有人有兴趣,这里是代码:
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
可能需要一些微调(使步长和最小值配置与设计可见性),但它非常有效的时刻。
为什么不在运行时检查此属性? – Arseny 2010-06-10 09:49:00
@Arseny:该属性是真的标签“可以”使用省略号,如果文字大。对于一个小文本没有任何改变,但该属性仍然是真实的。 – 2010-06-10 14:52:58