Good afternoon.
Can anyone tell me how I delete all the rows of a Listview, leaving only the first 10 rows? And if there are less than 10 lines, then show all the lines that are.
I know how to delete the selected line. Please tell me how to delete all lines in the listview, except for the first 10 lines. Thanks.
For Each eachItem As ListViewItem In ListView1.SelectedItems
ListView1.Items.Remove(eachItem)
Next eachItem
When using this code, any item with an index higher than 10 will be removed out of the ListView:
For Each item In ListView1.Items
If ListView1.Items.IndexOf(item) > 9 Then
ListView1.Items.Remove(item)
End If
Next
Related
I have two listboxes, one contains an approved list of items, which is used to loop through listbox1 items to see if the items in there contain any of the items from the approved items (for example '.txt')
If the item doesn't contain the approved item then it's removed.
The problem is when I add a second approved item, for example '.pdf', all of the '.txt' files are removed from the list as well because of the loop.
Any ideas would be appreciated to fix this, I want to keep all of the items that are approved in the listbox! Thanks
For Each item As Object In Me.lstApprovedItems.Items
For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1
If Not ListBox1.Items(i).Contains(item) Then
ListBox1.Items.RemoveAt(i)
End If
Next
Well, yes, if 'item' is ".pdf", then ListBox1.Items(i) doesn't contain 'item' if ListBox1.Items(i) is ".txt". Instead of a slow inefficient nested For loop, try using some LINQ instead. How about something like this?
Dim list1 as list(of string) = (From Item in Listbox1.Items Select Item Where lstApprovedItems.items.Contains(Item)).tolist()
My syntax on the Contains part may be a bit off, but you can see where I'm going.
That will then give you a list of all the items currently in Listbox1.items which are also in lstApprovedItems.items. You could then rebind your listbox1 to use that.
So I have a userform that has a:
listbox called "lstentries"
radio button called "Delete".
Also, the "Start" is the name of the cell that is the top left corner of the table of rows.
I already set the code so it can select multiple entries in the listbox. However, when I select multiple entries it only deletes the first row that it comes to. So i tried making a while loop so it keeps deleting the selected ones, but that's not working either. I get the "Run-time error '1004' - Method 'Range' of object'_Global' failed"
Hopefully someone can help me out. Here is the snippet of the code where it deletes the rows. Thanks in advance.
If optDelete.Value = True Then
Dim delete As Range
Do While True
Set delete = Range("start").Offset(lstEntries.ListIndex, 0)
delete.EntireRow.delete Shift:=xlUp
Loop
End If
In the list box you can run through the list of items. Since you are allowing for multiple items to be selected and deleted, you will have to adjust the index as items are removed.
The list box is 0-indexed which means it starts at 0 and not 1.
' set the to go through all items in the list
For listIndexCount = 0 To lstEntries.ListCount
' check to make sure the index count is not greater or equal to the total number of items
' this is needed because the ListCount will change if items are deleted
If listIndexCount >= lstEntries.ListCount Then
Exit Sub
End If
' check to see if the item is selected
If lstEntries.Selected(listIndexCount) Then
' remove item
lstEntries.RemoveItem (listIndexCount)
' subtract 1 to account for removed item
listIndexCount = listIndexCount - 1
End If
Next
Another option in cases like this is to iterate through in reverse order. When starting at the bottom of the list you do not have to worry about adjusting the index since the items further up are not affected by removing an item below them.
I have a panel with about six controls on the panel. I wanted to remove the controls from the panel and finally did so with MyPanel.Clear(). But before that I tried the following code that runs from a button click:
For Each b As Control In MyItem.MyPanel.Controls
MyItem.MyPanel.Controls.Remove(b)
Next
I would click the button and watch it, as well as the MyItem.MyPanel.Controls.Count in debug. As it went through, the count would reduce: to 5 to 4 to 3, then it would exit. If I clicked the button again it would remove two more, then the last one on the third click, so they all fit the bill and were all removed without changing anything. Why did it take three clicks? I'm obviously missing something simple here, I think, but I don't know what it is, and I'd really like to understand it. If I had to remove specific controls, it looks like I would have had a problem.
I ran into this issue myself and its odd it even lets you do it as you're modifying the collection in the loop you are referring to.This should be a better method.
If you like to remove them based on type
For i = Panel1.Controls.Count - 1 To 0 Step -1
If TypeOf Panel1.Controls(i) Is Label Then
Panel1.Controls.Remove(Panel1.Controls(i))
End If
Next
Odd that VB.NET even lets you do this, but essentially what you are doing is editing the collection you are iterating through. To better understand, pretend you are using a regular for loop from 1 to 6, at the first iteration you are removing object 1, leaving you with 5 objects, making the old number 2 object the first. The next iteration you remove the 2nd thing, which used to be the third, and so on. Most languages this is a run-time error.
What is happening is that you are deleting the controls starting from the first position and moving to the last. If the list has 6 records and you start deleting them like you are, programatically you are saying:
remove(0)
remove(1)
...
remove(5)
While you are doing that, the list is getting smaller. Once you delete the first item it drops from 6 positions to 5, then 4, then 3, etc. So midway through your code it tries to remove item at location 3 (4th item), but since you already removed 3 items, the list's size only contains 3 items and that position does not exist.
To properly remove them all, you would have to start at the back of the list and move to the front.
Perhaps something like:
For i As Integer = (MyItem.MyPanel.Controls.Count- 1) To 0 Step -1
MyItem.mypanel.Controls(i).Dispose()
Next
I'm working with a listbox in vb.net and am trying to remove all items from the listbox that don't contain specific text at the click of a button. Here's my code:
Dim i As Integer
For i = 0 To ListBoxPrePublish.Items.Count - 1
If InStr(ListBoxPrePublish.Items(i), "-8-") > 0 = False Then
ListBoxPrePublish.Items.RemoveAt(i)
Exit For
End If
Next
This only removes 1 item at a time though. How can I tweak this to remove all items that don't contain "-8-" at once?
EDIT: in case anyone asks, the listbox items list is growing rather large so I'm adding a sort feature so users can widdle down their options if they want to. That's why I'm not filtering anything before adding to the listbox
Here is the complete code for backward loop I mentioned in the comments - it should work:
For i as Integer = ListBoxPrePublish.Items.Count - 1 To 0 Step -1
If Not ListBoxPrePublish.Items(i).Contains("-8-") Then
ListBoxPrePublish.Items.RemoveAt(i)
End If
Next
No, I do not know of any RemoveRange type functionality. And be advised that you will need to loop through the listbox Items collection backwards as you remove items or you will get index exceptions, because once you remove something it will mess up the index values of all the remaining items in the iterator.
I've tried searching on stackoverflow and implementing code that I found but to no avail. As the text changes I want to use the current text in the textbox to filter through the listview items (so the items that are not closed to being matched get removed) and it only leaves me with whatever is contained in the columns.
Here's an example of what I mean:
Search: "Georges"
|1|Anderson Silva|Anderson silva is the champion in the ...|
|2|Georges St-Pierre| Georges is the champion in the ...|
|3|Georges Sotoropolis| Georges Sotoropolis is a fighter in the lightweight division|
With this search, only rows 2 and three would be returned. The first row would be omitted and not displayed. Once I erase the terms, then it would get displayed.
Here is the code that I currently have:
Private Sub tbSearch_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearch.TextChanged
lwArticles.BeginUpdate()
If tbSearch.Text.Trim().Length = 0 Then
'Clear listview
lwArticles.Clear()
'If nothing is in the textbox make all items appear
For Each item In originalListItems
lwArticles.Items.Add(item)
Next
Else
'Clear listview
lwArticles.Clear()
'Go through each item in the original list and only add the ones which contain the search text
For Each item In originalListItems
If item.Text.Contains(tbSearch.Text) Then
lwArticles.Items.Add(item)
End If
Next
End If
lwArticles.EndUpdate()
End Sub
It seems to be working but I cannot see the listview items once I enter something in the tbSearch. The scrollbar gets smaller / larger depending if there are more / less items due to the search being executed. My problem seems that they are not visible
Thank you!
Listview.Clear wipes out the items, columns, and groups. It appears you only want to wipe out the items, so call lwArticles.Items.Clear instead of lwArticles.Clear
I would suggest another approach - first create a DataTable based on your original items. Then create a DataView and assign that as DataSource of your ListView. Now you can change DataView.RowFilter, and your list will update automatically, so only items matching the filter will show up. Using this approach you don't need to recreate everything from scratch every time, and your TextChanged becomes very simple and maintainable - it just changes RowFilter, if a corresponding DataView has already been created.
Here's the final answer of my question:
Private originalListItems As New List(Of ListViewItem) 'this gets populated on form load
Private Sub tbSearch_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearch.TextChanged
lwArticles.BeginUpdate()
If tbSearch.Text.Trim().Length = 0 Then
'Clear listview
lwArticles.Items.Clear()
'If nothing is in the textbox make all items appear
For Each item In originalListItems
lwArticles.Items.Add(item)
Next
Else
'Clear listview
lwArticles.Items.Clear()
'Go through each item in the original list and only add the ones which contain the search text
For Each item In originalListItems
If item.Text.Contains(tbSearch.Text) Then
lwArticles.Items.Add(item)
End If
Next
End If
lwArticles.EndUpdate()
End Sub
Credits to Dan-o for explaining that lwArticles.Clear() will clear everything. He notified me that I needed lwArticles.Items.Clear() to clear only the items in the listview.