How do I delete the selected items in a list in VB?

To delete the selected content from the list, you can use the Remove method, which requires passing the element to be deleted as a parameter.

Here is an example code:

' 创建一个 List
Dim myList As New List(Of String)
myList.Add("apple")
myList.Add("banana")
myList.Add("orange")

' 输出当前 List 的内容
Console.WriteLine("当前 List 的内容:")
For Each item As String In myList
    Console.WriteLine(item)
Next

' 用户选择要删除的元素的索引
Console.Write("请选择要删除的元素的索引:")
Dim index As Integer = CInt(Console.ReadLine())

' 删除选中的元素
myList.RemoveAt(index)

' 输出删除后的 List 的内容
Console.WriteLine("删除后的 List 的内容:")
For Each item As String In myList
    Console.WriteLine(item)
Next

In the example above, the user is required to input the index of the element to be deleted, and then use the RemoveAt method to delete that element. Finally, the content of the List after deletion is displayed.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds