replace listbox item

Help, I can't figure out how to replace an item a listbox.
I can delete the selected item and add the nw item but there has to be a better way
Thanks

[168 byte] By [garmhappy] at [2008-2-26]
# 1
There is no "Replace" method. You have to call Remove or RemoveAt and then call either Add or Insert.

Hope this helps,
the V-Bee

shoagMSFT at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
You can replace an item but you don't call a method to do it. You just set that item to the value you want, e.g. Me.ListBox1.Items(0) = "New Item". The item must exist already or an exception will be thrown.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Something along these lines may help as it it will allow you to replace Full exact match items or partial matches in items and chnage either just the first occurence or all occurences.

As its searching for string matches in the list and not specific index positions your unlikely to generate an exception if trying to reference a specific item in the list which doesn't exist.
Function
ReplaceListBoxItems(ByRef ListBox As ListBox, ByVal OriginalItem As String, ByVal ReplaceItem As String, Optional ByVal FirstInstance As Boolean = True, Optional ByVal PartialMatch As Boolean = False) As Boolean

Dim iIndex As Integer = 0

Try

For iIndex = 0 To ListBox.Items.Count - 1

If PartialMatch = False Then
If ListBox.Items(iIndex) = OriginalItem Then
ListBox.Items(iIndex) = ReplaceItem
End If
Else
If InStr(ListBox.Items(iIndex), OriginalItem) Then
'//Replace only the string that matched
ListBox.Items(iIndex) = ListBox.Items(iIndex).Replace(OriginalItem, ReplaceItem)
End
If
End If

'//Only Replace first Instance it finds rather than searching through entire list items

If FirstInstance = True Then
Exit For
End If

Next

Catch ex As Exception

End Try

End Function

spotty at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...