Visual Basic Online Course - ListView, Element not found while removing items

Visual Basic Online Course

ListView,  Element not found while removing items

In VB6 If you tried to remove items from ListView Control, you may get this error :
Run-Time Error '35601':
 Element not found.

Example :

Code :
'Visual Basic 6.0
'Solution for ListView Control
Option Explicit
Dim i As Integer
Private Sub Form_Load()
For i = 1 To 10
ListView1.ListItems.Add Key:="lv_" & i, Text:="ListItem #" & i
Next
End Sub
Private Sub Command1_Click()
For i = 1 To ListView1.ListItems.Count
If ListView1.ListItems("lv_" & i).Selected Then
ListView1.ListItems.Remove i
End If
Next
End Sub
view raw gistfile1.vb hosted with ❤ by GitHub

Error :
Executing the previous example will cause error '35601' when trying to remove the next Item from list view items.

Solution :
Simple error trap will fix the error
'Visual Basic 6.0
'solutions for ListView control
On Error Resume Next
For i = 1 To ListView1.ListItems.Count
If Listiew1.ListItems("lv_" & i).Selected Then
ListView1.ListItems.Remove i
End If
Next
view raw gistfile1.vb hosted with ❤ by GitHub



Comments

VB6 Popular Posts

Visual Basic Online Course - Function Keys (F1 to F12)

VB 0.6 Copy, Paste, Cut and Create Folders

Visual Basic Online Course - Run-time error '3021' : Either BOF or EOF is True, or the current record has been deleted.