一、需求
使ListView实现单选模式
二、实现
1、ListView控件属性FullRowSelect设置为True
2、ListView控件属性MultiSelect设置为False
3、ListView控件属性CheckBoxes设置为True
4、为ListView添加ItemCheck事件,并在事件中调用如下方法
/// <summary> /// 报表列表框-单选模式的实现,列表框应关闭多选模式,放在事件_ItemCheck下 /// </summary> /// <param name="lv"></param> /// <param name="e"></param> public static void ListViewSingleSelectedAndChecked(ListView lv, ItemCheckEventArgs e) { if (e.CurrentValue == CheckState.Unchecked) { int count = lv.Items.Count; ListViewItem item = lv.Items[e.Index]; if (!item.Checked) { for (int i = 0; i < count; i++) { if (i != e.Index) { ListViewItem item1 = lv.Items[i]; item1.Checked = false; item1.Selected = false; } else { lv.Items[i].Selected = true; } } } } if (e.CurrentValue == CheckState.Checked) { e.NewValue = CheckState.Unchecked; if (lv.Items[e.Index].Selected) { lv.Items[e.Index].Selected = false; } } }
三、示例
private void listView_1_ItemCheck(object sender, ItemCheckEventArgs e) { ListViewSingleSelectedAndChecked(listView_1, e); }
已有 7644 位网友参与,快来吐槽:
发表评论