Wenn Sie den SelectionMode auf None festlegen, treten einige seltsame Probleme auf, z. B. das Click-Ereignis implementieren zu müssen. Sie können die SelectionMode-Gruppe auf Single setzen und dann eine Klasse erstellen, die die CheckedListBox nur mit OnDrawItem überschreibt. Beachten Sie, dass Sie den Selected-Status deaktivieren müssen, um eine ausgewählte Darstellung zu deaktivieren, und legen Sie die Farben so fest, wie Sie möchten. Sie können die ursprüngliche Farbe von der Steuerung erhalten, wie ich hier getan habe. Das ist, was ich mir ausgedacht habe und sollte dir helfen, es so aussehen zu lassen, wie du es willst.
public partial class EnhancedCheckedListBox : CheckedListBox
{
/// <summary>Overrides the OnDrawItem for the CheckedListBox so that we can customize how the items are drawn.</summary>
/// <param name="e">The System.Windows.Forms.DrawItemEventArgs object with the details</param>
/// <remarks>A CheckedListBox can only have one item selected at a time and it's always the item in focus.
/// So, don't draw an item as selected since the selection colors are hideous.
/// Just use the focus rect to indicate the selected item.</remarks>
protected override void OnDrawItem(DrawItemEventArgs e)
{
Color foreColor = this.ForeColor;
Color backColor = this.BackColor;
DrawItemState s2 = e.State;
//If the item is in focus, then it should always have the focus rect.
//Sometimes it comes in with Focus and NoFocusRect.
//This is annoying and the user can't tell where their focus is, so give it the rect.
if ((s2 & DrawItemState.Focus) == DrawItemState.Focus)
{
s2 &= ~DrawItemState.NoFocusRect;
}
//Turn off the selected state. Note that the color has to be overridden as well, but I just did that for all drawing since I want them to match.
if ((s2 & DrawItemState.Selected) == DrawItemState.Selected)
{
s2 &= ~DrawItemState.Selected;
}
//Console.WriteLine("Draw " + e.Bounds + e.State + " --> " + s2);
//Compile the new drawing args and let the base draw the item.
DrawItemEventArgs e2 = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, s2, foreColor, backColor);
base.OnDrawItem(e2);
}
}
Scheint, wie es wäre effizienter sein, checkedListBox1.IndexFromPoint (ex, ey) im MouseDown-Handler anstelle von o f Durchlaufen der GetItemRectangle-Ergebnisse. – Eyal