【问题标题】:VB.NET multi color LabelVB.NET 多色标签
【发布时间】:2011-12-26 23:22:02
【问题描述】:

我想在单个标签控制器中为文本设置多种颜色

例如

label1.Text = " $ 480.00 "

我想要的是红色字符 $ 和蓝色 $ 之后的其他数字或字符。

我不能为数字和$ 设置单独的标签。

【问题讨论】:

标签: vb.net


【解决方案1】:

标签本身无法做到这一点,因此您可以使用只读 RichTextBox 控件,也可以制作自己的标签控件。

最简单的形式:

Public Class ColorLabel
  Inherits Control

  Private _Money As Decimal = 0

  Property Money() As Decimal
    Get
      Return _Money
    End Get
    Set(ByVal value As Decimal)
      _Money = value
      Me.Invalidate()
    End Set
  End Property

  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    MyBase.OnPaint(e)

    Dim moneyText As String = String.Format("{0:N2}", _Money)
    Dim dollarWidth As Integer = TextRenderer.MeasureText(e.Graphics, "$", Me.Font).Width
    Dim moneyWidth As Integer = TextRenderer.MeasureText(e.Graphics, moneyText, Me.Font).Width

    TextRenderer.DrawText(e.Graphics, "$", Me.Font, New Point(Me.ClientSize.Width - (dollarWidth + moneyWidth + 2), 2), Color.Red)
    TextRenderer.DrawText(e.Graphics, moneyText, Me.Font, New Point(Me.ClientSize.Width - (moneyWidth + 2), 2), Color.Blue)
  End Sub

End Class

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多