C#, VB.NET <ListBox> ListBoxItem 각 각 글자색 다르게 하기.listbox fore color 지정하기

2020. 2. 18. 09:08C#

728x90
반응형

<ListBox> ListBoxItem 각 각 글자색 다르게 하기.

listbox fore color 지정하기

 

<C#>

void listBox1_SetColor(object sender, DrawItemEventArgs e)
{
    try
    {
        e.DrawBackground();
        Brush myBrush = Brushes.White;

        int sayi = Convert.ToInt32(((ListBox)sender).Items[e.Index].ToString());
        if (sayi > 100)
        {
            myBrush = Brushes.Red;

        }
        else
        {
            myBrush = Brushes.Green;
        }

        e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
        e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);

        e.DrawFocusRectangle();
    }
    catch
    {

    }

}

 

 

<VB.NET>

Public Sub New()
    InitializeComponent()

    ListBox1.DrawMode = DrawMode.OwnerDrawFixed

End Sub
Private Sub listbox_colorset(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem

e.DrawBackground()

Dim myBrush As Brush = Brushes.White

' listbox의 값을 정수형으로 저장한다.
Dim sayi As Integer = Convert.ToInt32(TryCast(sender, ListBox).Items(e.Index).ToString)

' listbox의 아이템이 100 이상이면 빨간색, 이하면 초록색으로 브러쉬를 지정한다.
If sayi > 100 Then

myBrush = Brushes.Red
Else
myBrush = Brushes.Green

End If

' listbox의 값을 기준으로 brush를 그린다.
e.Graphics.DrawString(
TryCast(sender, ListBox).Items(e.Index).ToString,
e.Font,
myBrush,
e.Bounds,
StringFormat.GenericDefault)

e.DrawFocusRectangle()

End Sub

 

 

 

728x90
반응형