Reputation: 559
I wanted to have multiple color for text in single label controller
e.g.
label1.Text = " $ 480.00 "
What I want is character $
in Red color and other digits or character after $
in color blue.
I cannot have separate labels for digits and $
.
Upvotes: 2
Views: 4889
Reputation: 81620
The label itself can't do that, so you can either use a read-only RichTextBox control, or just make your own label control.
In it's simplest form:
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
Result:
Upvotes: 3