首页 > 编程语言 > Winform ComboBox如何独立绘制下拉选项的字体颜色
2020
11-25

Winform ComboBox如何独立绘制下拉选项的字体颜色

众所周知,cbo1.ForeColor会同时改变cbo1.Text和下拉选项字体颜色。

独立绘制下拉选项字体颜色,ForeColor 只对 cbo1.Text 颜色有效的办法,示例:

private void cb7_DrawItem(object sender, DrawItemEventArgs e)
{
  DrawCboItems(sender, e);
}

private void cb7_SelectedIndexChanged(object sender, EventArgs e)
{
  SetCboTextColor(sender);
}

private void SetCboTextColor(object sender)
{
  ComboBox cbx = sender as ComboBox;
  if (cbx.Text == "+")
    cbx.ForeColor = Color.Red;
  else
    cbx.ForeColor = Color.Black;
}

private void DrawCboItems(object sender, DrawItemEventArgs e)
{
  ComboBox cbo = sender as ComboBox;

  //初始化字体和背景色
  Pen fColor = new Pen(Color.Black);
  Pen bColor = new Pen(Color.White);

  switch (e.Index)
  {     //下拉选项的索引
    case 1:
    {
      fColor = new Pen(Color.Red);
      break;
    }
  }

  e.Graphics.FillRectangle(bColor.Brush, e.Bounds);
  e.Graphics.DrawString((string)cbo.Items[e.Index], this.Font, fColor.Brush, e.Bounds);
}

以上就是Winform ComboBox如何独立绘制下拉选项的字体颜色的详细内容,更多关于Winform ComboBox绘制字体颜色的资料请关注自学编程网其它相关文章!

编程技巧