如何检测带有自动省略号的 System.Windows.Forms.Label 是否确实显示省略号?
我有一个 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
可能需要一些微调(使步长和最小值可通过设计器可见的属性进行配置),但目前效果很好。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,你的标签可以是多行的。在这种情况下, label.PreferredWidth 没有帮助。但你可以使用:
In fact, your Lable could be multiline. In this case, label.PreferredWidth wouldn't help. But you can use: