从 VB.net 中的 ListBox 中删除项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14379438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:34:56  来源:igfitidea点击:

Removing items from a ListBox in VB.net

vb.net

提问by user1945423

I have two ListBox1and ListBox2. I have inserted items into a ListBox2with the following code by selecting ListBox1item:

我有两个ListBox1ListBox2。我ListBox2通过选择ListBox1项目将项目插入到具有以下代码的a 中:

da6 = New SqlDataAdapter("select distinct(component_type) from component where   component_name='" & ListBox1.SelectedItem() & "'", con)
da6.Fill(ds6, "component")
For Each row As DataRow In ds6.Tables(0).Rows
    ListBox2.Items.Add(row.Field(Of String)("component_type"))
Next

But when I reselect another item of ListBox1then ListBox2shows preloaded items and now loaded item together. I want only now loaded item to be displayed in listbox. I used this code but problem not solved:

但是当我重新选择另一个项目时,ListBox1会同时ListBox2显示预加载的项目和现在加载的项目。我只想在列表框中显示现在加载的项目。我使用了这段代码,但问题没有解决:

For i =0 To ListBox2.items.count - 1
    ListBox2.Items.removeAt(i)
Next

OR listbox2.items.clear()is also not working..

OR listbox2.items.clear()也不起作用..

How can I clear all items in the ListBox2?

如何清除ListBox2? 中的所有项目?

回答by Tim Schmelter

Use simply:

简单地使用:

ListBox2.Items.Clear()
  • To take your last edit into account: Do that beforeyou add the new items
  • 考虑您上次的编辑:添加新项目之前执行此操作

MSDN: ListBox.ObjectCollection.Clear

微软: ListBox.ObjectCollection.Clear

Removes all items from the collection.

从集合中删除所有项目。

Note that the problem with your approach is that RemoveAtchanges the index of all remaining items.

请注意,您的方法的问题在于RemoveAt更改了所有剩余项目的索引。

When you remove an item from the list, the indexes change for subsequent itemsin the list. All information about the removed item is deleted. You can use this method to remove a specific item from the list by specifying the index of the item to remove from the list. To specify the item to remove instead of the index to the item, use the Remove method. To remove all items from the list, use the Clear method.

当您从列表中删除一个项目时,列表中后续项目索引会发生变化。删除有关已删除项目的所有信息。您可以使用此方法通过指定要从列表中删除的项目的索引来从列表中删除特定项目。要指定要删除的项目而不是项目的索引,请使用 Remove 方法。要从列表中删除所有项目,请使用 Clear 方法

If you want to use RemoveAtanyway, you can go backwards, for example with:

如果你RemoveAt无论如何都想使用,你可以倒退,例如:

a for-loop:

一个for循环:

For i As Int32 = ListBox2.Items.Count To 0 Step -1
    ListBox2.Items.RemoveAt(i)
Next

or a while

while

While ListBox2.Items.Count > 0
    ListBox2.Items.RemoveAt(ListBox2.Items.Count - 1)
End While

old C# code

旧的 C# 代码

for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
    ListBox2.Items.RemoveAt(i);

while(ListBox2.Items.Count > 0)
    ListBox2.Items.RemoveAt(ListBox2.Items.Count - 1);
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
    ListBox2.Items.RemoveAt(i);

while(ListBox2.Items.Count > 0)
    ListBox2.Items.RemoveAt(ListBox2.Items.Count - 1);

回答by Zatiel

This code worked for me:

这段代码对我有用:

ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)

ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)

回答by Fredrik M?rk

If you only want to clear the list box, you should use the Clear (winforms| wpf| asp.net) method:

如果只想清除列表框,则应使用 Clear ( winforms| wpf| asp.net) 方法:

ListBox2.Items.Clear()

回答by Jake

There is a simple method for deleting selected items, and all these people are going for a hard method:

有一个简单的方法可以删除选定的项目,而所有这些人都在寻求一个困难的方法:

lstYOURVARIABLE.Items.Remove(lstYOURVARIABLE.SelectedItem)

I used this in Visual Basic mode on Visual Studio.

我在 Visual Studio 的 Visual Basic 模式下使用了它。

回答by Jim

Here's the code I came up with to remove items selected by a user from a listbox It seems to work ok in a multiselect listbox (selectionmode prop is set to multiextended).:

这是我想出的用于从列表框中删除用户选择的项目的代码它似乎在多选列表框中工作正常(selectionmode prop 设置为 multiextended)。:

Private Sub cmdRemoveList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdRemoveList.Click
    Dim knt As Integer = lstwhatever.SelectedIndices.Count
    Dim i As Integer
    For i = 0 To knt - 1
        lstwhatever.Items.RemoveAt(lstwhatever.SelectedIndex)
    Next
End Sub

回答by Diego Lara

Already tested by me, it works fine

我已经测试过了,效果很好

For i =0 To ListBox2.items.count - 1
ListBox2.Items.removeAt(0)
Next

回答by RainerJ

I think your ListBox already clear with ListBox2.Items.Clear(). The problem is that you also need to clear your dataset from previous results with ds6.Tables.Clear().

我认为你的 ListBox 已经用 ListBox2.Items.Clear() 清除了。问题是您还需要使用 ds6.Tables.Clear() 从以前的结果中清除数据集。

Add this in your code:

在您的代码中添加以下内容:

da6 = New SqlDataAdapter("select distinct(component_type) from component where   component_name='" & ListBox1.SelectedItem() & "'", con)
    ListBox1.Items.Clear()    ' clears ListBox1
    ListBox2.Items.Clear()    ' clears ListBox2
    ds6.Tables.Clear()        ' clears DataSet  <======= DON'T FORGET TO DO THIS
da6.Fill(ds6, "component")
For Each row As DataRow In ds6.Tables(0).Rows
    ListBox2.Items.Add(row.Field(Of String)("component_type"))
Next

回答by Heinrich

This worked for me.

这对我有用。

Private Sub listbox_MouseDoubleClick(sender As Object, e As MouseEventArgs) 
        Handles listbox.MouseDoubleClick
        listbox.Items.RemoveAt(listbox.SelectedIndex.ToString())
    End Sub

回答by Bernardo Ravazzoni

   Dim ca As Integer = ListBox1.Items.Count().ToString
    While Not ca = 0
        ca = ca - 1
        ListBox1.Items.RemoveAt(ca)
    End While