【问题标题】:How change the color of SelectedItem in CheckedListBox in WindowsForms?如何在 Windows 窗体中更改 CheckedListBox 中选定项的颜色?
【发布时间】:2010-01-25 08:07:37
【问题描述】:

我想更改 C# WindowsForms 中 CheckedListBox 中检查的项目的颜色。

谁能帮我解决这个问题!

【问题讨论】:

    标签: c# winforms checkedlistbox


    【解决方案1】:

    这应该可以帮助您入门。我将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);
        }
    }
    

    【讨论】:

      【解决方案2】:

      感谢 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); 
              }
            }
          }
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 2016-11-06
        • 1970-01-01
        • 2016-04-24
        • 1970-01-01
        • 2021-09-20
        相关资源
        最近更新 更多