I have a listview that has multiple entries and each entry has 2 subitems. I am wanting to know how to remove each item in the listview where the subitem(1) equals a certain string.
What would be the best way to do this?
thanks
You can't use a for..each loop to remove items. after you remove the first item, the for...each is broken.
Try this:
Dim pos As Int32
Dim listItem As ListViewItem
For pos = lvw.Items.Count - 1 To 0 Step -1
listItem = lvw.Items(pos)
If listItem.SubItems(1).Text = "testvalue" Then
lvw.Items.Remove(listItem)
End If
Next
Dim listItem As ListViewItem
Dim someName As String
For Each listItem In lvw.Items
If listItem.Text = someName Then
lvw.Items.Remove(listItem)
' If you only want to remove one item with that Text
' you can put an Exit For right here
End If
Next
You can try something like this.
For Each listItem As ListViewItem In ListView1.Items
If listItem.SubItems.Item(1).Text = "SomeName" Then
listItem.Remove()
End If
Next
This is probably the easiest way of removing all the list items.
Do While YOURITEMLIST.Items.Count <> 0
YOURITEMLIST.Items.Remove(YOURITEMLIST.Items(0))
Loop
Dim x As Integer = 0
For Each item6 As ListViewItem In ListView4.Items
Dim f As String = item6.SubItems(1).Text
Dim ind As Integer = item6.Index
For Each item7 As ListViewItem In ListView4.Items
Dim f2 As String = item7.SubItems(1).Text
' MsgBox(f & " 2nd value " & f2)
If (f = f2) Then
x = x + 1
' MsgBox(x & "= time matched" & f)
If (x > 1) Then
MsgBox("delete here")
ListView4.Items.Remove(item6)
End If
End If
Next
x = 0
Next
Related
Want to unselect items on condition. I noticed that when do setselected then i get error on enxt loop because index was changed after first loop, therefore i decided to refresh and make everytime loop again but after one item is unselected i have invinitive loop for second one. How to accomplish that properly? Listbox is binded to datasource.
sss:
For i As Integer = 0 To ListBox1.SelectedItems.Count - 1
Dim variationswert1 As DataRowView = ListBox1.SelectedItems(i)
Dim name As String = ListBox1.GetItemText(variationswert1)
if name = "TR8" Then
ListBox1.SetSelected(i, False)
ListBox1.Refresh()
GoTo sss
End If
Next
EDIT: (for further discussion):
For i = 0 To ListBox1.Items.Count - 1
If ListBox1.SelectedItems.Contains(ListBox1.Items(i)) Then
MsgBox(ListBox1.GetItemText(ListBox1.Items(i)))
MsgBox(ListBox1.SelectedValue.ToString)
ListBox1.SetSelected(i, False)
End If
Next
ListBox1.Refresh()
Your problem is that you are iterating through the Selected Items, but since you are going to change that array, any loop you will run will eventually fail :
For Each will fail because you will modify the collection
For i = 0 to ... will fail because you will remove an item and therfore end in an OutOfRangeException (or something like that)
The idea would be to :
Run through all your items :
If the item is selected and the item text is "TR8", unselect this item
And here would be the code :
Dim FoundItem As Object 'Or Whatever class the items are
For i = 0 to ListBox1.Items.Count - 1
If ListBox1.SelectedItems.Contains(ListBox1.Items(i)) AndAlso _
ListBox1.GetItemText(ListBox1.Item(i)) = "TR8" Then
FoundItem = ListBox1.Items(i)
ListBox1.SetSelected(i, False)
MsgBox(FoundItem.ToString())
End If
Next
ListBox1.Refresh()
Is looping all thi items, not onlys the selected, viable?
sss:
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.SelectedItems.Contains(ListBox1.Items(i)) Then
Continue For
End If
Dim variationswert1 As DataRowView = ListBox1.Items(i)
Dim name As String = ListBox1.GetItemText(variationswert1)
if name = "TR8" Then
ListBox1.SetSelected(i, False)
ListBox1.Refresh()
GoTo sss
End If
Next
EDIT:
Using for each and leaving the rest the same:
For Each item In ListBox1.Items
If Not ListBox1.SelectedItems.Contains(item) Then
Continue For
End If
Dim variationswert1 As DataRowView = item
Dim name As String = ListBox1.GetItemText(variationswert1)
If name = "TR8" Then
ListBox1.SetSelected(ListBox1.Items.IndexOf(item), False)
ListBox1.Refresh()
GoTo sss
End If
Next
Simplifing it, I may have not understood it, but it seems that you want to unslect all the listbox items that their text is "TR8"
For Each item In ListBox1.Items
If ListBox1.SelectedItems.Contains(item) And ListBox1.GetItemText(item) = "TR8" Then
ListBox1.SetSelected(ListBox1.Items.IndexOf(item), False)
ListBox1.Refresh()
End If
Next
EDIT 2:
So without for each and
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.SelectedItems.Contains(ListBox.Items(i)) And ListBox1.GetItemText(ListBox.Items(i)) = "TR8" Then
ListBox1.SetSelected(i, False)
ListBox1.Refresh()
End If
Next
I got multiline textbox and ListView
textbox contains :
[1000]
name=John
number0=78569987
[1001]
name=Sara
number0=89768980
number1=77897545
TextBox2.Text = TextBox2.Text.Replace("[", "this what i want")
Dim lines As New List(Of String)
lines = TextBox2.Lines.ToList
Dim FilterText = "this what i want"
For i As Integer = lines.Count - 1 To 0 Step -1
If Not Regex.IsMatch(lines(i), FilterText) Then
lines.RemoveAt(i)
End If
Next
TextBox2.Lines = lines.ToArray
TextBox2.Text = TextBox2.Text.Replace("this what i want", "")
TextBox2.Text = TextBox2.Text.Replace("]", "")
ListBox1.Items.AddRange(TextBox2.Lines)
For Each x As String In ListBox1.Items
Dim II As New ListViewItem
II.Text = x
ListView1.Items.Add(II)
Next
I cant use the same way to insert numbers and names because some ids contain number0 number 1 and some contain only number 0 ,, so how can I insert their numbers ?
Thanks in advance.
Check the below code with comments. You might have to modify slightly to fit the data.
Check this question and it's linked questions for similar thing.
Edit: Note that this only copies from rows in richtextbox to different columns in listview, so it will work for the examples you provided. I hope you can refine this logic to account for specific columns as per the data coming in the richtextbox.
Dim lines As New List(Of String)
lines = TextBox2.Lines.ToList
'Add 1st row to the listview
ListView1.Items.Add(New ListViewItem())
'Use Counter to determine row#
Dim j As Integer = 0
'Loop through the items
For i As Integer = 0 To lines.Count - 1
'Check if it's 1st item i.e. ID and add as text (i.e. at Index 0)
If lines(i).StartsWith("[") Then
ListView1.Items(j).Text = lines(i).Substring(1, lines(i).Length - 2)
'Check if contains other columns with attributes
ElseIf lines(i).Contains("=") Then
ListView1.Items(j).SubItems.Add(lines(i).Substring(lines(i).IndexOf("=") + 1))
'Check if it's an empty record, and add new row to listview
Else
j = j + 1
ListView1.Items.Add(New ListViewItem())
End If
Next
Is there a way to read the string that is in a listbox item, prompt the user to enter the string, and if the user enters it in correctly SELECT the next item until there are no items left to select? I am making this in VB.NET. Help would be greatly appreciated.
You could do something like this:
Dim items = ListBox1.Items.Cast(Of Object)().Select(Function(x) x >= x.ToString).ToList()
If items.Contains(TextBox1.Text) Then
Dim nextIndex = items.IndexOf(TextBox1.Text) + 1
If nextIndex < items.Count Then 'There is next item
Dim nextItem = items(nextIndex).ToString()
'Do something with your nextItem
End If
End If
Basically, you try to get all texts in your ListBox by doing this:
Dim items = ListBox1.Items.Cast(Of Object)().Select(Function(x) x >= x.ToString).ToList()
Then you check if an input string is among the text in your ListBox:
If items.Contains(TextBox1.Text) Then
.
.
.
End If
If it does, you get the index of the next item:
Dim nextIndex = items.IndexOf(TextBox1.Text) + 1
And if the index of the next item is not exceeding the ListBox's number of of elements, you get the item in the next index:
If nextIndex < items.Count Then 'There is next item
Dim nextItem = items(nextIndex).ToString()
'Do something with your nextItem
End If
Your next string is in the nextItem, do something with it.
Or, without populating the whole item but still getting the same result, you could add additional Where LINQ clause (as suggested by Codexer):
Dim item = ListBox1.Items.Cast(Of Object)().Select(Function(x) x >= x.ToString).Where(Function(y) y = TextBox1.Text).FirstOrDefault()
If Not item Is Nothing Then
Dim nextIndex = ListBox1.Items.IndexOf(item) + 1
If nextIndex < ListBox1.Items.Count Then 'There is next item
Dim nextItem = ListBox1.Items(nextIndex)
'Do something with your nextItem
End If
End If
I'm working on a project where the program gets items from a table and puts it in a listview. Now I want to check if the top 3 items matches the items in another listview i have set up and return a message whether or not listview1 contains any items from listview2. I can do this for the whole listview1 but I want to check the top 3 only.
For Each li As ListViewItem In ListView2.Items
Dim liToFind As ListViewItem = ListView1.FindItemWithText(li.Text)
If Not IsNothing(liToFind) Then
Using New Centered_MessageBox(Me)
MessageBox.Show(li.Text & " has released a new episode!", "New release", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Using
End If
Next
This is the code for what I have so far, im checking the text of the items in listview2 to see if it matches any of the items in listview1, can't figure out how to do it with the top 3 only though.
Help would be greatly apriciated!
Here is an example - obviously you can be cleverer here .
Dim checkrows As Integer = 3
Dim numFound As Integer
For row As Integer = 0 To Math.Min(checkrows - 1, ListView1.Items.Count - 1)
Dim text As String = ListView1.Items(row).Text
Dim found As Boolean
For Each item2 As ListViewItem In ListView2.Items
If text.Contains(item2.Text) Then
found = True
Exit For
End If
Next
If found Then numFound += 1
Next
If numFound > 0 Then MessageBox.Show("At least one of the first " & checkrows.ToString & " rows in ListView1 matches a row in ListView2")
If numFound = checkrows Then MessageBox.Show("The first " & checkrows.ToString & " rows in ListView1 match a row in ListView2")
This worked for me, thanks AltF4 but yours didn't really work out for me. Ty for putting your time into this anyway <3
Dim checkrows As Integer = 3
Dim numFound As Integer
For row As Integer = 0 To Math.Min(checkrows - 1, ListView1.Items.Count - 1)
Dim text As String = ListView1.Items(row).Text
Dim found As Boolean
For Each item2 As ListViewItem In ListView2.Items
If text.Contains(item2.Text) Then
found = True
Exit For
End If
Next
If found Then numFound += 1
Next
If numFound > 0 Then MessageBox.Show("At least one of the first " & checkrows.ToString & " rows in ListView1 matches a row in ListView2")
If numFound = checkrows Then MessageBox.Show("The first " & checkrows.ToString & " rows in ListView1 match a row in ListView2")
I'm trying to get the row indices based on my selected cells on a DataGridView. How can I do that in VB.NET?
This is what I have:
Dim iRowIndex As Integer
For i = 0 To Me.grdTransaction.SelectedCells.Item(iRowIndex)
iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex.ToString()
Dim s As String = Me.grdTransaction.SelectedRows(i).Cells("DataGridViewTextBoxColumn6").Value
aList.Add(s)
MsgBox("Row index " & iRowIndex)
Next
Thanks to #matzone I have figured it out:
Dim iRowIndex As Integer
For i As Integer = 0 To Me.grdTransaction.SelectedCells.Count - 1
iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex
aList.Add(Me.grdTransaction.Rows(iRowIndex).Cells("DataGridViewTextBoxColumn6").Value)
MsgBox("Row index " & Format(iRowIndex))
Next
DGV.CurrentRow.Index
Will work even if selectionMode = CellSelect
I don't think I'm understanding the question. Why does
iRowIndex = grdTransaction.SelectedRow.RowIndex
not work?
You can try this ..
Dim iRowIndex As Integer
Dim s As String
For i as Integer = 0 To Me.grdTransaction.SelectedCells.Count -1
iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex.ToString()
aList.Add(Me.grdTransaction.SelectedRows(i).Cells("DataGridViewTextBoxColumn6").Value)
MsgBox("Row index " & format(iRowIndex))
Next