我有ListBox
,其中包含一些数据。
ListBox1.Items.Add("Set 1")
ListBox1.Items.Add("1.1")
ListBox1.Items.Add("1.2")
ListBox1.Items.Add("1.3")
等
Set 1
和Set 2
包含3个子数据,而Set 3
和Set 4
没有任何子数据。选择Set 3
或Set 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
答案 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