当在选中列表框中单击一个项目时,它会突出显示。如何防止这种突出显示效果?
我可以连接到 SelectedIndexChanged 事件并清除选择,但突出显示仍然发生并且您会看到一个光点。事实上,如果您按住鼠标单击,在单击复选框区域后从不释放它,则选择会保持突出显示,直到您释放鼠标按钮。我基本上想完全摆脱这种突出效果。
当在选中列表框中单击一个项目时,它会突出显示。如何防止这种突出显示效果?
我可以连接到 SelectedIndexChanged 事件并清除选择,但突出显示仍然发生并且您会看到一个光点。事实上,如果您按住鼠标单击,在单击复选框区域后从不释放它,则选择会保持突出显示,直到您释放鼠标按钮。我基本上想完全摆脱这种突出效果。
使用以下内容:
private void checkedListBox1__SelectedIndexChanged(object sender, EventArgs e)
{
checkedListBox1.ClearSelected();
}
除了你仍然得到虚线位之外,这将做到这一点。
this.checkedListBox1.SelectionMode = System.Windows.Forms.SelectionMode.None;
尽管现在您无法单击复选框...所以您必须执行以下操作:
private void checkedListBox1_Click(object sender, EventArgs e)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemRectangle(i).Contains(checkedListBox1.PointToClient(MousePosition)))
{
switch (checkedListBox1.GetItemCheckState(i))
{
case CheckState.Checked:
checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
break;
case CheckState.Indeterminate:
case CheckState.Unchecked:
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
break;
}
}
}
}
如果这一切都不是你所追求的……你总是可以自己做一个。它是一个相当简单的控件。
将 SelectionMode 设置为 None 有一些奇怪的问题,例如必须实现 Click 事件。您可以将 SelectionMode 设置为 single,然后创建一个仅使用 OnDrawItem 覆盖 CheckedListBox 的类。请注意,为了关闭选定的外观,您必须关闭 Selected 状态并将颜色设置为您想要的颜色。您可以像我在这里所做的那样从控件中获取原始颜色。这就是我想出的,应该让你开始让它看起来像你想要的那样。
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);
}
}
哦,酷添加将来自Hath的答案中的所有代码放在一个
checkedListBox1_MouseMove(object sender, MouseEventArgs e)
如果鼠标按钮位在开关中,则添加
case CheckState.Checked:
if (e.Button == MouseButtons.Right)
{
checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
}
break;
和
case CheckState.Unchecked:
if (e.Button == MouseButtons.Left)
{
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
}
break;
它会在您按下左键移动鼠标时检查突出显示的项目,并用右键取消突出显示它们
我在这里给出答案有点晚了。无论如何,这将取消选中所有复选框并删除突出显示效果:
foreach (int i in clb.CheckedIndices) //clb is your checkListBox
clb.SetItemCheckState(i, CheckState.Unchecked);
clb.SelectionMode = System.Windows.Forms.SelectionMode.None;
clb.SelectionMode = System.Windows.Forms.SelectionMode.One;