CheckedListBox 控件 - 仅在单击实际复选框时选中复选框 [英] CheckedListBox Control - Only checking the checkbox when the actual checkbox is clicked

查看:42
本文介绍了CheckedListBox 控件 - 仅在单击实际复选框时选中复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我正在开发的一个小应用程序中使用 CheckedListBox 控件.这是一个很好的控制,但有一件事困扰着我;我无法设置属性,以便它仅在我实际选中复选框时检查该项目.克服这个问题的最佳方法是什么?我一直在考虑从复选框的左侧获取鼠标点击的位置.这部分有效,但如果我点击一个空白区域,离左侧足够近,仍会检查当前选定的项目.对此有什么想法吗?

I'm using a CheckedListBox control in a small application I'm working on. It's a nice control, but one thing bothers me; I can't set a property so that it only checks the item when I actually check the checkbox. What's the best way to overcome this? I've been thinking about getting the position of the mouseclick, relative from the left side of the checkbox. Which works partly, but if I would click on an empty space, close enough to the left the current selected item would still be checked. Any ideas regarding this?

推荐答案

嗯,它很丑,但是你可以通过钩住 CheckedListBox.MouseDownCheckedListBox.ItemCheck 如下所示

Well, it is quite ugly, but you could calculate mouse hit coordinates against rectangles of items by hooking on CheckedListBox.MouseDown and CheckedListBox.ItemCheck like the following

/// <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;
        }
    }
}

这篇关于CheckedListBox 控件 - 仅在单击实际复选框时选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆