[WinForm] Listbox 문자에 색 입히기

2016. 6. 28. 13:00C#/WinForm

    private void listBoxTest_DrawItem(object sender, DrawItemEventArgs e)

          {

             if (listBoxTest.Items.Count == 0)

               return;

 

              // owner draw를 사용하여 ListBox에 색칠하기

             Brush myBrush;

             string strText = listBoxTest.Items[e.Index].ToString();

             if (strText.Contains("ERROR") == true)

            {

               // ERROR문자가 있으면 붉게 칠한다.

               myBrush = Brushes.Red;

            }

            else if (strText.Contains("SUCCESS") == true)

            {

               // SUCCESS문자가 있으면 파랗게 칠한다.

              myBrush = Brushes.Blue;

            }

            else

            {

               // 위 두가지 경우 외에는 모두 검게 칠한다.

               myBrush = Brushes.Black;

            }

 

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

           e.DrawFocusRectangle();

        }