首先设置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();
        }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
  • 2021-11-28
  • 2021-09-28
猜你喜欢
  • 2022-01-21
  • 2021-08-12
  • 2022-02-10
  • 2021-10-14
  • 2021-03-31
  • 2021-07-12
  • 2021-09-12
相关资源
相似解决方案