如何在WindowsForms中更改CheckedListBox中SelectedItem的颜色?

我想在C#WindowsForms中更改CheckedListBox中chedked项的颜色。

任何人都可以帮我解决这个问题!

这应该让你开始。 我已经将CheckedListBox子类化并覆盖了绘图事件。 结果是列表中的所有选中项都以红色背景绘制。

如果您希望复选框后面的区域也是不同的颜色,请在调用base.OnDrawItem之前使用e.Graphics.FillRectangle

 class ColouredCheckedListBox : CheckedListBox { protected override void OnDrawItem(DrawItemEventArgs e) { DrawItemEventArgs e2 = new DrawItemEventArgs ( e.Graphics, e.Font, new Rectangle(e.Bounds.Location, e.Bounds.Size), e.Index, e.State, e.ForeColor, this.CheckedIndices.Contains(e.Index) ? Color.Red : SystemColors.Window ); base.OnDrawItem(e2); } } 

感谢Jon让我走上正确的道路,因为我有同样的愿望:让项目的文本颜色对于复选框的3种状态中的每一种都不同。

我想出了CheckedListBox的这个子类。 它会更改项目文本,而不是背景颜色。 它允许用户在设计时或代码中设置3种颜色。

它还修复了我在设计器中查看控件时遇到错误的问题。 我还必须克服我认为在您的解决方案中会发生的问题,如果选择了该项,则base.OnDrawItem方法将删除在重写的OnDrawItem方法中设置的颜色选项。 我这样做是以牺牲所选项目不再具有彩色背景为代价的,删除了e.State的部分,表示它被选中,以便在base.OnDrawItem中,它不会成为选定项目的外观。 虽然我认为这是好的,因为用户将看到仍然指示哪个被选中的焦点矩形。

希望这可能对其他人有用。 在网上查看时,我没有找到一个有凝聚力的解决方案(甚至只是一个完整的OnDrawItem方法)。

 using System; using System.Windows.Forms; using System.Drawing; namespace MyNameSpace { ///  /// This is a CheckedListBox that allows the item's text color to be different for each of the 3 states of the corresponding checkbox's value. /// Like the base CheckedListBox control, you must handle setting of the indeterminate checkbox state yourself. /// Note also that this control doesn't allow highlighting of the selected item since that obscures the item's special text color which has the special meaning. But /// the selected item is still known to the user by the focus rectangle it will have surrounding it, like usual. ///  class ColorCodedCheckedListBox : CheckedListBox { public Color UncheckedColor { get; set; } public Color CheckedColor { get; set; } public Color IndeterminateColor { get; set; } ///  /// Parameterless Constructor ///  public ColorCodedCheckedListBox() { UncheckedColor = Color.Green; CheckedColor = Color.Red; IndeterminateColor = Color.Orange; } ///  /// Constructor that allows setting of item colors when checkbox has one of 3 states. ///  /// The text color of the items that are unchecked. /// The text color of the items that are checked. /// The text color of the items that are indeterminate. public ColorCodedCheckedListBox(Color uncheckedColor, Color checkedColor, Color indeterminateColor) { UncheckedColor = uncheckedColor; CheckedColor = checkedColor; IndeterminateColor = indeterminateColor; } ///  /// Overriden draw method that doesn't allow highlighting of the selected item since that obscures the item's text color which has desired meaning. But the /// selected item is still known to the user by the focus rectangle being displayed. ///  ///  protected override void OnDrawItem(DrawItemEventArgs e) { if (this.DesignMode) { base.OnDrawItem(e); } else { Color textColor = this.GetItemCheckState(e.Index) == CheckState.Unchecked ? UncheckedColor : (this.GetItemCheckState(e.Index) == CheckState.Checked ? CheckedColor : IndeterminateColor); DrawItemEventArgs e2 = new DrawItemEventArgs (e.Graphics, e.Font, new Rectangle(e.Bounds.Location, e.Bounds.Size), e.Index, (e.State & DrawItemState.Focus) == DrawItemState.Focus ? DrawItemState.Focus : DrawItemState.None, /* Remove 'selected' state so that the base.OnDrawItem doesn't obliterate the work we are doing here. */ textColor, this.BackColor); base.OnDrawItem(e2); } } } }