Jür*_*ock 6 c# label ellipsis winforms
我有一个Windows窗体应用程序,我在标签中显示一些客户端数据.我设置了label.AutoEllipsis = true.
如果文本比标签长,它看起来像这样:
Some Text
Some longe... // label.Text is actually "Some longer Text"
// Full text is displayed in a tooltip
Run Code Online (Sandbox Code Playgroud)
这就是我想要的.
但现在我想知道标签是否在运行时使用AutoEllipsis功能.我怎么做到这一点?
解
感谢max.现在我能够创建一个控件,试图将整个文本放在一行中.如果有人有兴趣,这是代码:
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
Run Code Online (Sandbox Code Playgroud)
可能需要一些微调(使步骤大小和最小值可配置设计师可见的属性),但它目前运作良好.
max*_*max 14
private static bool IsShowingEllipsis(Label label)
{
return label.PreferredWidth > label.Width;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
事实上,你的标签可以是多行的。在这种情况下, label.PreferredWidth 无济于事。但是你可以使用:
internal static bool IsElipsisShown(this Label @this)
{
Size sz = TextRenderer.MeasureText(@this.Text, @this.Font, @this.Size, TextFormatFlags.WordBreak);
return (sz.Width > @this.Size.Width || sz.Height > @this.Size.Height);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3714 次 |
| 最近记录: |