delete selected entry in CheckedListBox

373 views Asked by At

Good day! so delete all the ticked entries from CheckedListBox

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

Tell me how to delete the selected entry in the CheckedListBox?

enter image description here

in the picture the record with the name 3 is highlighted. Is it possible and how to delete it without putting a check?

5

There are 5 answers

0
Karen Payne On

Here is a simple example for

  • Removing all checked items
  • Removing the current item disregarding the checked state.

Form code

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

enter image description here

0
Arshad Ali On
Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
    If isDeleted = False Then
        isDeleted = True
        CheckedListBox1.Items.RemoveAt(CheckedListBox1.SelectedIndex)
    Else
        isDeleted = False
    End If

End Sub
0
Arshad Ali On

Dim isDeleted As Boolean = False

Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
    If isDeleted = False Then
        isDeleted = True
        CheckedListBox1.Items.RemoveAt(CheckedListBox1.SelectedIndex)
    Else
        isDeleted = False
    End If

End Sub
0
Prabahar On

Private Sub CheckedListBox1_Click(sender As Object, e As EventArgs) Handles CheckedListBox1.Click

Dim i As String = ""

i = CheckedListBox1.SelectedItem

    If i = Nothing Then
        Exit Sub
    Else
        CheckedListBox1.Items.Remove(i)
    End If
End Sub
1
Dishen Jariwala On
    For index As Integer = CheckedListBox1.Items.Count - 1 To 0 Step -1
        If CheckedListBox1.GetItemChecked(index) Then
            CheckedListBox1.Items.RemoveAt(index)
        End If
    Next