簡體   English   中英

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

[英]How change the color of SelectedItem in CheckedListBox in WindowsForms?

我想在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
  {
  /// <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); 
        }
      }
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM