从ListBox中的选定项目中删除3行

时间:2014-12-24 18:49:39

标签: vb.net listbox

我有ListBox,其中包含一些数据。

ListBox1.Items.Add("Set 1")
ListBox1.Items.Add("1.1")
ListBox1.Items.Add("1.2")
ListBox1.Items.Add("1.3")
  • 设置1
    • 1.1
    • 1.2
    • 1.3
  • 设置2
    • 2.1
    • 2.2
    • 2.3
  • 设置3
  • Set 4
  • 设置1
    • 1.1
    • 1.2
    • 1.3
  • 设置1
    • 1.1
    • 1.2
    • 1.3

Set 1Set 2包含3个子数据,而Set 3Set 4没有任何子数据。选择Set 3Set 4后,它将删除所选数据。

' Remove Set 3 and Set 4 selected data
ListBox1.Items.Remove(ListBox1.SelectedItem)

用户选择Set 1后,应删除整个Set 1包含其子数据,Set 2相同。无论在1/1.1/1.2/1.3还是2/2.1/2.2/2.3上选择了用户,都应从ListBox中删除所选内容的全部内容。到目前为止,我已经这样做了:

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    If ListBox1.SelectedIndex <> -1 Then
        Select Case ListBox1.SelectedItem.ToString
            Case "Set 1"
                For i = ListBox1.SelectedItems.Count - 1 To 0 Step -1
                    For teller = 0 To 2
                        ListBox1.Items.RemoveAt(i)
                    Next
                Next
            Case "Set 2"
                ' Etc
            Case Else
                ' Remove Set 3 and Set 4 selected data
                ListBox1.Items.Remove(ListBox1.SelectedItem)
        End Select
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

尽管您应该使用树来显示子项目,但这是您的问题的解决方案

编辑:好的,我将此调整为您的命名,这应该有效。只需将它放在Button5 Click事件上即可。您的评论另有说明可能会进一步解释一下吗?

    Dim mainitemList As New List(Of String)
    Dim subitemList As New List(Of String)
    Dim currentMain As String

    For Each selItem As String In ListBox1.SelectedItems
        If Not selItem.Contains(".") Then
            mainitemList.Add(selItem)

            currentMain = selItem.Replace("Set ", String.Empty)

            For Each lbSubItems As String In ListBox1.Items
                If lbSubItems.Contains(currentMain & ".") Then
                    subitemList.Add(lbSubItems)
                End If
            Next
        Else
            subitemList.Add(selItem)
        End If
    Next

    For Each subItem As String In subitemList
        ListBox1.Items.Remove(subItem)
    Next

    For Each mainItem As String In mainitemList
        ListBox1.Items.Remove(mainItem)
    Next

答案 1 :(得分:0)

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click Dim count As Integer = ListBox1.SelectedIndex

    If ListBox1.SelectedIndex <> -1 Then
        Select Case Me.ListBox1.SelectedItem.SubString(0, 1)                
            Case "Set 1"
                For i = ListBox1.SelectedItems.Count - 1 To 0 Step -1
                    For teller = 0 To 1
                        ListBox1.Items.RemoveAt(count)
                    Next
                Next
            Case Else
                MsgBox("Please select set to delete!")
        End Select
    End If
End Sub

If ListBox1.SelectedIndex <> -1 Then Select Case Me.ListBox1.SelectedItem.SubString(0, 1) Case "Set 1" For i = ListBox1.SelectedItems.Count - 1 To 0 Step -1 For teller = 0 To 1 ListBox1.Items.RemoveAt(count) Next Next Case Else MsgBox("Please select set to delete!") End Select End If End Sub