2011-11-11 4 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

이 [게시물] [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