C# 如何摆脱checkedlistbox选择高亮效果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/334626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get rid of checkedlistbox selection highlighting effect?
提问by Emrah Diril
When an item is clicked in the checkedlistbox, it gets highlighted. How can I prevent this highlighting effect?
在选中列表框中单击某个项目时,它会突出显示。如何防止这种突出显示效果?
I can hook into the SelectedIndexChanged event and clear the selection, but the highlighting still happens and you see a blip. In fact, if you hold down the mouse click, never releasing it after you clicked on the checkbox area, the selection remains highlighted until you release the mouse button. I basically want to get rid of this highlighting effect altogether.
我可以连接到 SelectedIndexChanged 事件并清除选择,但突出显示仍然发生并且您会看到一个 blip。事实上,如果您按住鼠标单击,在单击复选框区域后永远不要松开,则选择将保持突出显示,直到您松开鼠标按钮。我基本上想完全摆脱这种高亮效果。
采纳答案by Hath
this will do it apart from you still get the dotted line bit.
除了你仍然得到虚线位之外,这将做到这一点。
this.checkedListBox1.SelectionMode = System.Windows.Forms.SelectionMode.None;
although now you can't click the check boxes... so you'll have to do something like so:
虽然现在你不能点击复选框......所以你必须做这样的事情:
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;
}
}
}
}
if all that isn't what your after.. you can always just make your own one. its a fairly simple control.
如果这一切都不是你想要的……你总是可以自己做一个。它是一个相当简单的控件。
回答by Mark
Setting the SelectionMode to None has some weird issues like having to implement the Click event. You can leave the SelectionMode set to single and then create a class that overrides the CheckedListBox with just OnDrawItem. Note that in order to turn off a selected appearance, you have to turn off the Selected state and set the colors to what you want. You can get the original color from the control like I did here. This is what I came up with and should get you started in making it appear however you want.
将 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);
}
}
回答by victoria
Use the following:
使用以下内容:
private void checkedListBox1__SelectedIndexChanged(object sender, EventArgs e)
{
checkedListBox1.ClearSelected();
}
回答by Trilby_Rob
ooh cool add put all the code from answer from Hath in a
哦,很酷的添加将来自 Hath 的答案中的所有代码放入一个
checkedListBox1_MouseMove(object sender, MouseEventArgs e)
add in if mouse button bit in to the switch
添加如果鼠标按钮位进入开关
case CheckState.Checked:
if (e.Button == MouseButtons.Right)
{
checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
}
break;
and
和
case CheckState.Unchecked:
if (e.Button == MouseButtons.Left)
{
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
}
break;
and it will check the highlighted items as you move the mouse with the left button pressed and un-highlight them with the right
当您按住左键移动鼠标时,它会检查突出显示的项目,并使用右键取消突出显示它们
回答by user3437460
I am a little late to give an answer here. Anyway, this will uncheck allcheckboxes and remove the highlightingeffect:
我在这里给出答案有点晚了。无论如何,这将取消选中所有复选框并删除突出显示效果:
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;
- Uncheck all checkboxes
- Disable checkListBox's selection
- Enable checkListBox's selection
- 取消选中所有复选框
- 禁用 checkListBox 的选择
- 启用 checkListBox 的选择