如何删除CheckedListBox中勾选条目及未勾选的高亮选中条目
CheckedListBox选中条目(高亮无勾选)删除实现(VB)
完全可以无需勾选直接删除高亮选中的条目,勾选状态和选中高亮状态是控件的两个独立属性,操作互不影响。
你之前提供的是删除所有已勾选条目的实现,读取的是控件的CheckedItems集合:
For Each item In CheckedListBox1.CheckedItems.OfType(Of String)().ToList() CheckedListBox1.Items.Remove(item) Next item
如果要删除下图中高亮的选中条目,读取SelectedIndices或者SelectedItems集合即可:
常用实现代码
1. 单选模式下删除单个选中条目
默认SelectionMode为One时使用:
' 先判断是否存在选中条目,避免索引越界报错 If CheckedListBox1.SelectedIndex <> -1 Then CheckedListBox1.Items.RemoveAt(CheckedListBox1.SelectedIndex) End If
2. 多选模式下批量删除选中条目
当SelectionMode设置为MultiSimple/MultiExtended允许多选高亮时使用,倒序遍历避免索引偏移漏删:
' 从最后一个选中项往前遍历删除 For i As Integer = CheckedListBox1.SelectedIndices.Count - 1 To 0 Step -1 CheckedListBox1.Items.RemoveAt(CheckedListBox1.SelectedIndices(i)) Next
注意事项
- 如果控件绑定了数据源,不能直接操作Items集合删除,需要修改对应数据源后调用控件的刷新方法
- 可根据业务需求在删除前增加弹窗确认逻辑,避免用户误删
内容的提问来源于stack exchange,提问作者Lider13
相关产品推荐
相关产品推荐

