CheckedListBox 当中项的状态改变

来源:互联网 发布:淘宝女装店铺 编辑:程序博客网 时间:2024/05/17 05:05

首先,说下我需要实现的功能,就是把获得数据Id显示在CheckedListBox上(单击空白处不响应),如果在CheckedListBox上勾选的项就加入到list集合中,取消勾选状态就从集合中移除。

之前我碰到一个问题,如图:

我采用SelectedIndexChanged 和MouseClick事件,都不能解决我碰到的问题。

解决办法:

 void lstMotes_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (lstMotes.IndexFromPoint(lstMotes.PointToClient(Cursor.Position).X,
                      lstMotes.PointToClient(Cursor.Position).Y) == -1)
            {//这边判断如果点击空白的地方不触发事件
                e.NewValue = e.CurrentValue;
            }
            else
            {//这边开始判断集合当中是不是已经有ID,如果没有就加入
                string temp = lstMotes.SelectedItem.ToString();
                if (!listM_moteeui.Contains(temp))
                {
                    listM_moteeui.Add(temp);
                }
                else
                    listM_moteeui.Remove(temp);

            }
        }

0 0
原创粉丝点击