2010-01-25 8 views

답변

7

시작해야합니다. 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); 
    } 
} 
4

내가 같은 욕망을 가지고로서 옳은 길을 저를 얻었다 감사 존 : 항목의 텍스트 색상이 체크 박스의 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); 
     } 
     } 
    } 
    } 
관련 문제