Reputation: 61
I have two ListBox1
and ListBox2
. I have inserted items into a ListBox2
with the following code by selecting ListBox1
item:
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 ListBox1
then ListBox2
shows 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:
For i =0 To ListBox2.items.count - 1
ListBox2.Items.removeAt(i)
Next
OR
listbox2.items.clear()
is also not working..
How can I clear all items in the ListBox2
?
Upvotes: 4
Views: 133268
Reputation: 1
This can be also checked. Selected item from the list can be deleted using for loop, but error occurs as soon as list item removed to avoid this problem just jump from the loop and start fresh selected list to be remove.
Private Sub BtnDelete_ClickButtonArea(Sender As Object, e As MouseEventArgs) Handles BtnDelete.ClickButtonArea
If LstTest.SelectedIndex > -1 Then
jmp1:
If LstTest.SelectedItems.Count > 0 Then
For i = 0 To LstTest.SelectedItems.Count - 1
For Each SI In LstTest.SelectedItems
LstTest.Items.Remove(SI)
GoTo jmp1
Next
Next
End If
End If
End Sub
Upvotes: 0
Reputation: 36
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
Upvotes: 0
Reputation: 71
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().
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
Upvotes: 1
Reputation: 127
Dim ca As Integer = ListBox1.Items.Count().ToString
While Not ca = 0
ca = ca - 1
ListBox1.Items.RemoveAt(ca)
End While
Upvotes: -1
Reputation: 3379
Already tested by me, it works fine
For i =0 To ListBox2.items.count - 1
ListBox2.Items.removeAt(0)
Next
Upvotes: 2
Reputation: 51
This code worked for me:
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
Upvotes: 4
Reputation: 41
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.
Upvotes: 4
Reputation: 51
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).:
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
Upvotes: 2
Reputation: 460058
Use simply:
ListBox2.Items.Clear()
MSDN: ListBox.ObjectCollection.Clear
Removes all items from the collection.
Note that the problem with your approach is that RemoveAt
changes the index of all remaining items.
When you remove an item from the list, the indexes change for subsequent items in 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.
If you want to use RemoveAt
anyway, you can go backwards, for example with:
a for
-loop:
For i As Int32 = ListBox2.Items.Count To 0 Step -1
ListBox2.Items.RemoveAt(i)
Next
or a while
While ListBox2.Items.Count > 0
ListBox2.Items.RemoveAt(ListBox2.Items.Count - 1)
End While
old C# code
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);
Upvotes: 9
Reputation: 158309
If you only want to clear the list box, you should use the Clear (winforms | wpf | asp.net) method:
ListBox2.Items.Clear()
Upvotes: 4