Oxy*_*ron 10 c# checkedlistbox winforms
我在我正在使用的小应用程序中使用CheckedListBox控件.这是一个很好的控制,但有一件事困扰我; 我无法设置属性,以便在我实际选中复选框时仅检查项目.克服这个问题的最佳方法是什么?我一直在考虑从复选框的左侧获取鼠标点击的位置.这部分工作,但如果我点击一个空的空格,左边足够靠近,仍然会检查当前所选项目.关于这个的任何想法?
小智 12
我知道这个帖子有点旧,但我不认为提供另一个解决方案是个问题:
private void checkedListBox1_MouseClick(object sender, MouseEventArgs e)
{
if ((e.Button == MouseButtons.Left) & (e.X > 13))
{
this.checkedListBox1.SetItemChecked(this.checkedListBox1.SelectedIndex, !this.checkedListBox1.GetItemChecked(this.checkedListBox1.SelectedIndex));
}
}
Run Code Online (Sandbox Code Playgroud)
(带值CheckOnClick = True
).
你可以在矩形中使用那个东西,但为什么要使它变得更加复杂.
嗯,这是很丑陋,但你可以通过钩在计算鼠标击中坐标下的项目的矩形CheckedListBox.MouseDown
和CheckedListBox.ItemCheck
类似下面的
/// <summary>
/// In order to control itemcheck changes (blinds double clicking, among other things)
/// </summary>
bool AuthorizeCheck { get; set; }
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if(!AuthorizeCheck)
e.NewValue = e.CurrentValue; //check state change was not through authorized actions
}
private void checkedListBox1_MouseDown(object sender, MouseEventArgs e)
{
Point loc = this.checkedListBox1.PointToClient(Cursor.Position);
for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
{
Rectangle rec = this.checkedListBox1.GetItemRectangle(i);
rec.Width = 16; //checkbox itself has a default width of about 16 pixels
if (rec.Contains(loc))
{
AuthorizeCheck = true;
bool newValue = !this.checkedListBox1.GetItemChecked(i);
this.checkedListBox1.SetItemChecked(i, newValue);//check
AuthorizeCheck = false;
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
另一种解决方案是简单地使用Treeview.
将CheckBoxes设置为true,将ShowLines设置为false,将ShowPlusMinus设置为false,并且您与CheckedListBox基本相同.只有在单击实际的CheckBox时才会检查这些项目.
CheckedListBox更加简单,但TreeView提供了许多可能更适合您的程序的选项.
归档时间: |
|
查看次数: |
9515 次 |
最近记录: |