Emr*_*ril 14 c# checkedlistbox winforms
在checkedlistbox中单击某个项目时,它会突出显示.如何防止这种突出显示效果?
我可以挂钩到SelectedIndexChanged事件并清除选择,但突出显示仍然发生,你看到一个blip.事实上,如果您按住鼠标单击,在单击复选框区域后从不释放它,选择将保持突出显示,直到您释放鼠标按钮.我基本上想完全摆脱这种突出效果.
小智 25
使用以下内容:
private void checkedListBox1__SelectedIndexChanged(object sender, EventArgs e)
{
checkedListBox1.ClearSelected();
}
Run Code Online (Sandbox Code Playgroud)
Hat*_*ath 12
除了你仍然得到虚线位,这将做到这一点.
this.checkedListBox1.SelectionMode = System.Windows.Forms.SelectionMode.None;
Run Code Online (Sandbox Code Playgroud)
虽然现在你不能点击复选框...所以你必须这样做:
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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果所有这些都不是您的追求......您可以随时制作自己的产品.它是一个相当简单的控制.
小智 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);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13387 次 |
最近记录: |