在checkedlistbox中单击某个项目时,它会突出显示。如何防止这种突出显示效果?
我可以挂钩到SelectedIndexChanged事件并清除选择,但突出显示仍然发生,你会看到一个blip。实际上,如果您按住鼠标单击,在单击复选框区域后从不释放它,则选择将保持突出显示,直到您释放鼠标按钮。我基本上想完全摆脱这种突出效果。
答案 0 :(得分:24)
使用以下内容:
private void checkedListBox1__SelectedIndexChanged(object sender, EventArgs e)
{
checkedListBox1.ClearSelected();
}
答案 1 :(得分:12)
除了你仍然得到虚线位之外,它会做到这一点。
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;
}
}
}
}
如果所有这些都不是您的追求......您可以随时制作自己的产品。它是一个相当简单的控制。
答案 2 :(得分:7)
将SelectionMode设置为None有一些奇怪的问题,比如必须实现Click事件。您可以将SelectionMode设置为single,然后创建一个仅使用OnDrawItem覆盖CheckedListBox的类。请注意,要关闭所选外观,您必须关闭“选定”状态并将颜色设置为所需颜色。您可以像我在这里一样从控件中获取原始颜色。这就是我提出的,应该让你开始使它显得你想要的。
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);
}
}
答案 3 :(得分:0)
哦加凉 把所有来自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;
当按下左键移动鼠标时,它将检查突出显示的项目 并用正确的
取消突出显示它们答案 4 :(得分:0)
我在这里给出答案有点晚了。无论如何,这将取消选中所有复选框并删除突出显示效果:
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;