我想更改C#Windows窗体中CheckedListBox中检查的项目的颜色。任何人都可以帮我解决这个问题!
答案 0 :(得分:7)
这应该让你开始。我已经将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);
}
}
答案 1 :(得分:4)
感谢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);
}
}
}
}