Csharp/C#教程:更改ComboBox突出显示的颜色分享


更改ComboBox突出显示的颜色

我正在尝试在C# Windows Forms应用程序的ComboBox下拉列表中更改突出显示的颜色。 我在整个网页上搜索了一个答案,到目前为止我找到的最佳选项是在绘制所选项目时绘制所需颜色的矩形。

 Class Search { Public Search() { } private void addFilter() { ComboBox field = new ComboBox(); field.Items.AddRange(new string[] { "Item1", "item2" }); field.Text = "Item1"; field.DropDownStyle = ComboBoxStyle.DropDownList; field.FlatStyle = FlatStyle.Flat; field.BackColor = Color.FromArgb(235, 235, 235); field.DrawMode = DrawMode.OwnerDrawFixed; field.DrawItem += field_DrawItem; } private void field_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index >= 0) { ComboBox combo = sender as ComboBox; if (e.Index == combo.SelectedIndex) e.Graphics.FillRectangle(new SolidBrush(Color.Gray), e.Bounds ); else e.Graphics.FillRectangle(new SolidBrush(combo.BackColor), e.Bounds ); e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font, new SolidBrush(combo.ForeColor), new Point(e.Bounds.X, e.Bounds.Y) ); } } } 

这段代码的问题在于,一旦选择了下拉列表中的另一个项目,我绘制矩形的另一个项目仍然是我想要突出显示的颜色。 然后我试图保存最后绘制的项目并重绘它:

 Class Search { private DrawItemEventArgs lastDrawn; Public Search() { lastDrawn = null; } private void addFilter() { ComboBox field = new ComboBox(); field.Items.AddRange(new string[] { "Item1", "item2" }); field.Text = "Item1"; field.DropDownStyle = ComboBoxStyle.DropDownList; field.FlatStyle = FlatStyle.Flat; field.BackColor = Color.FromArgb(235, 235, 235); field.DrawMode = DrawMode.OwnerDrawFixed; field.DrawItem += field_DrawItem; } private void field_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index >= 0) { ComboBox combo = sender as ComboBox; if (e.Index == combo.SelectedIndex) { e.Graphics.FillRectangle(new SolidBrush(Color.Gray), e.Bounds); if(lastDrawn != null) lastDrawn.Graphics.FillRectangle(new SolidBrush(combo.BackColor), lastDrawn.Bounds ); lastDrawn = e; } else e.Graphics.FillRectangle(new SolidBrush(combo.BackColor), e.Bounds ); e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font, new SolidBrush(combo.ForeColor), new Point(e.Bounds.X, e.Bounds.Y) ); } } } 

由于lastDrawn.Bounds(不兼容的类型),此行返回错误

 lastDrawn.Graphics.FillRectangle(new SolidBrush(combo.BackColor), lastDrawn.Bounds ); 

我觉得改变下拉菜单的高亮颜色是不可能的。 提前致谢!

如果您在项目中的多个位置使用ComboBox ,那么一遍又一遍地重复相同的DrawItem事件代码是没有意义的。 只需将此类添加到项目中,您将拥有一个具有HightlightColor属性的新ComboBox控件,这将使整个项目中的控件更容易使用:

上述就是C#学习教程:更改ComboBox突出显示的颜色分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 class AdvancedComboBox : ComboBox { new public System.Windows.Forms.DrawMode DrawMode { get; set; } public Color HighlightColor { get; set; } public AdvancedComboBox() { base.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; this.HighlightColor = Color.Gray; this.DrawItem += new DrawItemEventHandler(AdvancedComboBox_DrawItem); } void AdvancedComboBox_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index < 0) return; ComboBox combo = sender as ComboBox; if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) e.Graphics.FillRectangle(new SolidBrush(HighlightColor), e.Bounds); else e.Graphics.FillRectangle(new SolidBrush(combo.BackColor), e.Bounds); e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font, new SolidBrush(combo.ForeColor), new Point(e.Bounds.X, e.Bounds.Y)); e.DrawFocusRectangle(); } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/998728.html

(0)
上一篇 2021年12月27日
下一篇 2021年12月27日

精彩推荐