Winform的ListBox项文字颜色交替变化设置

 首先设置ListBox的DrawMode为非默认风格,并设置DrawItem事件,如下:

        /// <summary>
        /// Listbox文字交替样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            Brush  myBrush = Brushes.Black  ; //初始化字体颜色=黑色
            if (listBox1.Items[e.Index].ToString().Substring(0, 1) == "我" || listBox1.Items[e.Index].ToString().Substring(0, 1) == "D")
            {
                myBrush = Brushes.Green;
            }
            else
            {
                myBrush = Brushes.Black;
            }
            e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font , myBrush, e.Bounds, null);
            e.DrawFocusRectangle();
        }

原文地址:https://www.cnblogs.com/mane/p/1955701.html