How to change SelectedItem color in CheckedListBox in WindowsForms?

I want to change the color of elements marked in CheckedListBox in C # WindowsForms.

Can someone help me solve this problem?

+4
source share
2 answers

This should help you get started. I have subclassed CheckedListBox and overridden the paint event. As a result, all marked items in the list are drawn in red.

From playing with this, if you want the area behind this flag to be a different color, use e.Graphics.FillRectangle before calling 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); } } 
+7
source

Thanks, John, who gave me the right path, because I had the same desire: that the text color of the element be different for each of the three states of the checkbox.

I came up with this subclass of CheckedListBox. It changes the text of the elements, not the background color. It allows you to set 3 colors by the user during development or in code, of course.

It also fixes the problem that I encountered when I had an error while viewing the control in the designer. I also had to overcome the problem that I think would occur in your solution, where, if an element is selected, the base.OnDrawItem method erases the color selection set in the overridden OnDrawItem method. I did this at the expense of the selected element that no longer has a colored background, deleting the e.State part, which says that it is selected so that in base.OnDrawItem it will not become the selected element. This is normal, although I think, since the user will see a focus rectangle that indicates what is selected.

Hope this can be helpful to others. I did not find much for a cohesive solution (not even just the complete OnDrawItem method) when I looked on the net.

 using System; using System.Windows.Forms; using System.Drawing; namespace MyNameSpace { /// <summary> /// This is a CheckedListBox that allows the item text color to be different for each of the 3 states of the corresponding checkbox 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 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 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); } } } } 
+4
source

Source: https://habr.com/ru/post/1299296/


All Articles