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

kap*_*pil 4 c# checkedlistbox winforms

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

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

Jon*_*gel 7

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

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Ken*_*erg 5

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

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

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

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

using System;
using System.Windows.Forms;
using System.Drawing;

namespace MyNameSpace
  {
  /// <summary>
  /// 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.
  /// </summary>
  class ColorCodedCheckedListBox : CheckedListBox
    {
    public Color UncheckedColor { get; set; }
    public Color CheckedColor { get; set; }
    public Color IndeterminateColor { get; set; }

    /// <summary>
    /// Parameterless Constructor
    /// </summary>
    public ColorCodedCheckedListBox() 
      {
      UncheckedColor = Color.Green;
      CheckedColor = Color.Red;
      IndeterminateColor = Color.Orange;
      }

    /// <summary>
    /// Constructor that allows setting of item colors when checkbox has one of 3 states.
    /// </summary>
    /// <param name="uncheckedColor">The text color of the items that are unchecked.</param>
    /// <param name="checkedColor">The text color of the items that are checked.</param>
    /// <param name="indeterminateColor">The text color of the items that are indeterminate.</param>
    public ColorCodedCheckedListBox(Color uncheckedColor, Color checkedColor, Color indeterminateColor) 
      {
      UncheckedColor = uncheckedColor;
      CheckedColor = checkedColor;
      IndeterminateColor = indeterminateColor;
      }

    /// <summary>
    /// 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.
    /// </summary>
    /// <param name="e"></param>
    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); 
        }
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)