Winform ComboBox如何独立绘制下拉选项的字体颜色
作者:沙奇码丶
这篇文章主要介绍了Winform ComboBox如何独立绘制下拉选项的字体颜色,帮助大家更好的理解和使用c# winform,感兴趣的朋友可以了解下
众所周知,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绘制字体颜色的资料请关注脚本之家其它相关文章!