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

编辑: admin 分类: c#语言 发布时间: 2022-01-18 来源:互联网

众所周知,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 = Colo【文章出处:http://www.1234xp.com/tbm.html转载请保留出处】r.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绘制字体颜色的资料请关注海外IDC网其它相关文章!

【文章原创作者:http://www.1234xp.com/yz.html 转载请保留连接】