2011-11-11 60 views
2

我想在單個標籤控制器中有多種顏色的文字VB.NET多色標籤

例如

label1.Text = " $ 480.00 "

我要的是在顏色藍色紅色和其他數字或字符$後字符$

我不能擁有單獨的數字標籤和$

+1

這是對勝利的形式,網頁,WPF? – ipr101

+0

[C#.NET標籤中的多種顏色]的可能重複(http://stackoverflow.com/questions/275836/multiple-colors-in-ac-sharp-net-label) –

+0

請閱讀此[post] [1 ] [1]:http://stackoverflow.com/questions/275836/multiple-colors-in-ac-sharp-net-label –

回答

3

標籤本身無法做到這一點,所以您可以使用只讀的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 

結果:

enter image description here