首页 > 解决方案 > 删除 CheckedListBox 中的选定条目

问题描述

再会!所以从 CheckedListBox 中删除所有勾选的条目

  For Each item In CheckedListBox1.CheckedItems.OfType(Of String)().ToList()
            CheckedListBox1.Items.Remove(item)
        Next item

告诉我如何删除 CheckedListBox 中选定的条目?

在此处输入图像描述

在图片中,名称为 3 的记录被突出显示。是否有可能以及如何在不检查的情况下删除它?

标签: vb.netcheckedlistbox

解决方案


这是一个简单的例子

  • 删除所有选中的项目
  • 删除当前项目而不考虑选中状态。

表格代码

Imports System.Globalization

Public Class Form1
    Private Sub RemoveCheckedButton_Click(sender As Object, e As EventArgs) Handles RemoveCheckedButton.Click

        If MonthCheckedListBox.CheckedItems.Count > 0 Then

            MonthCheckedListBox.CheckedItems.Cast(Of String)().ToList().
                ForEach(Sub(item) MonthCheckedListBox.Items.Remove(item))

            SetActive()

        End If

    End Sub
    Private Sub RemoveCurrentButton_Click(sender As Object, e As EventArgs) Handles RemoveCurrentButton.Click

        If MonthCheckedListBox.Items.Count > 0 Then
            MonthCheckedListBox.Items.RemoveAt(MonthCheckedListBox.SelectedIndex)
            SetActive()
        End If

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        MonthCheckedListBox.Items.AddRange(
            Enumerable.Range(1, 12).
                    Select(Function(index)
                               Return DateTimeFormatInfo.CurrentInfo.GetMonthName(index)
                           End Function).ToArray())

        SetActive()

    End Sub
    Private Sub SetActive()

        ActiveControl = MonthCheckedListBox

        If MonthCheckedListBox.Items.Count > 0 Then
            MonthCheckedListBox.SelectedIndex = 0
        End If

    End Sub
End Class

在此处输入图像描述


推荐阅读