ListView.ListItems 1 based or 0 based?

ListView.ListItems 1 based or 0 based?

Anonymous
Not applicable
510 Views
3 Replies
Message 1 of 4

ListView.ListItems 1 based or 0 based?

Anonymous
Not applicable
I was told here recently that .ListItems collection is 0 based
But I have tried numerous times to confirm that and can find no settings
which make it work.
I've tried all four settings for LV.View and none of them make it work
I've tried with and without .ColumnHeaders and neither makes it work....

Can anyone enlighten me on what I'm doing wrong?
I keep getting out of bounds error with oLV.ListItems.Item(0)

using this test ...
(you can replace LogEntry and LogError with Debug.Print
Sub EnumListItems(oLV As ListView)

Dim lChek As Long, sRpt As String
'norman yuan(whom I greatly respect and am sure is right) says ListItems
is zero based but I get out of bounds error on it
'For lChek = 0 To oLV.ListItems.Count - 1

CheckBase1:
On Error GoTo Base1Err
Debug.Print "Try 1 based collection"
For lChek = 1 To oLV.ListItems.Count
If oLV.ListItems.Item(lChek).Selected = True Then
sRpt = sRpt & "Item " & CStr(lChek) & " Selected " & vbCrLf
Else
sRpt = sRpt & "Item " & CStr(lChek) & " Not Selected " & vbCrLf
End If
Next lChek

CheckBase0:
On Error GoTo Base0Err
Debug.Print "Try 0 based collection"

For lChek = 0 To oLV.ListItems.Count - 1
If oLV.ListItems.Item(lChek).Selected = True Then
sRpt = sRpt & "Item " & CStr(lChek) & " Selected " & vbCrLf
Else
sRpt = sRpt & "Item " & CStr(lChek) & " Not Selected " & vbCrLf
End If
Next lChek

Debug.Print "Selected items oLV: " & vbCrLf & sRpt

ExitHere:
On Error GoTo 0
Exit Sub

EnumListItems_Error:
Err.Clear
Resume ExitHere
Base1Err:
Debug.Print "Base-1-Err"
Err.Clear
Resume CheckBase0
Base0Err:
Debug.Print "Base-0-Err"
Err.Clear
Resume ExitHere
End Sub
0 Likes
511 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
The first row is 1, with the headers (whether they are on or not) occupying
0 (altho thats not how you access the column headers). Similarly, the
checkbox uses up subitem(0) whether you use them or not, so your first
column is always at 1 and the number of columns is
listview.ListItems.Item(1).ListSubItems.Count

Also, the last row is given by listview.listitems.count, not
listview.listitems.count-1

Jon

"MP" wrote in message
news:5773489@discussion.autodesk.com...
I was told here recently that .ListItems collection is 0 based
But I have tried numerous times to confirm that and can find no settings
which make it work.
I've tried all four settings for LV.View and none of them make it work
I've tried with and without .ColumnHeaders and neither makes it work....

Can anyone enlighten me on what I'm doing wrong?
I keep getting out of bounds error with oLV.ListItems.Item(0)

using this test ...
(you can replace LogEntry and LogError with Debug.Print
Sub EnumListItems(oLV As ListView)

Dim lChek As Long, sRpt As String
'norman yuan(whom I greatly respect and am sure is right) says ListItems
is zero based but I get out of bounds error on it
'For lChek = 0 To oLV.ListItems.Count - 1

CheckBase1:
On Error GoTo Base1Err
Debug.Print "Try 1 based collection"
For lChek = 1 To oLV.ListItems.Count
If oLV.ListItems.Item(lChek).Selected = True Then
sRpt = sRpt & "Item " & CStr(lChek) & " Selected " & vbCrLf
Else
sRpt = sRpt & "Item " & CStr(lChek) & " Not Selected " & vbCrLf
End If
Next lChek

CheckBase0:
On Error GoTo Base0Err
Debug.Print "Try 0 based collection"

For lChek = 0 To oLV.ListItems.Count - 1
If oLV.ListItems.Item(lChek).Selected = True Then
sRpt = sRpt & "Item " & CStr(lChek) & " Selected " & vbCrLf
Else
sRpt = sRpt & "Item " & CStr(lChek) & " Not Selected " & vbCrLf
End If
Next lChek

Debug.Print "Selected items oLV: " & vbCrLf & sRpt

ExitHere:
On Error GoTo 0
Exit Sub

EnumListItems_Error:
Err.Clear
Resume ExitHere
Base1Err:
Debug.Print "Base-1-Err"
Err.Clear
Resume CheckBase0
Base0Err:
Debug.Print "Base-0-Err"
Err.Clear
Resume ExitHere
End Sub
0 Likes
Message 3 of 4

Anonymous
Not applicable
Thanks for the confirmation Jon,
Then ListView.ListItems is 1 based as is a collection
That's what I find in testing.
Maybe I misunderstood Norman, cause I know he knows his stuff!
If the Header is 0 as far as rows go, that doesn't make ListItems 0 based,
just theres a row before ListItems starts.
:-)

at least I can quit thinking I'm losing my mind because of the inability to
access item(0)
:-)
...so it must be due to some other cause ......!!
Thanks
Mark



"jon" wrote in message
news:5773552@discussion.autodesk.com...
The first row is 1, with the headers (whether they are on or not) occupying
0 (altho thats not how you access the column headers). Similarly, the
checkbox uses up subitem(0) whether you use them or not, so your first
column is always at 1 and the number of columns is
listview.ListItems.Item(1).ListSubItems.Count

Also, the last row is given by listview.listitems.count, not
listview.listitems.count-1

Jon

"MP" wrote in message
news:5773489@discussion.autodesk.com...
I was told here recently that .ListItems collection is 0 based
But I have tried numerous times to confirm that and can find no settings
which make it work.
I've tried all four settings for LV.View and none of them make it work
I've tried with and without .ColumnHeaders and neither makes it work....

Can anyone enlighten me on what I'm doing wrong?
I keep getting out of bounds error with oLV.ListItems.Item(0)

using this test ...
(you can replace LogEntry and LogError with Debug.Print
Sub EnumListItems(oLV As ListView)

Dim lChek As Long, sRpt As String
'norman yuan(whom I greatly respect and am sure is right) says ListItems
is zero based but I get out of bounds error on it
'For lChek = 0 To oLV.ListItems.Count - 1

CheckBase1:
On Error GoTo Base1Err
Debug.Print "Try 1 based collection"
For lChek = 1 To oLV.ListItems.Count
If oLV.ListItems.Item(lChek).Selected = True Then
sRpt = sRpt & "Item " & CStr(lChek) & " Selected " & vbCrLf
Else
sRpt = sRpt & "Item " & CStr(lChek) & " Not Selected " & vbCrLf
End If
Next lChek

CheckBase0:
On Error GoTo Base0Err
Debug.Print "Try 0 based collection"

For lChek = 0 To oLV.ListItems.Count - 1
If oLV.ListItems.Item(lChek).Selected = True Then
sRpt = sRpt & "Item " & CStr(lChek) & " Selected " & vbCrLf
Else
sRpt = sRpt & "Item " & CStr(lChek) & " Not Selected " & vbCrLf
End If
Next lChek

Debug.Print "Selected items oLV: " & vbCrLf & sRpt

ExitHere:
On Error GoTo 0
Exit Sub

EnumListItems_Error:
Err.Clear
Resume ExitHere
Base1Err:
Debug.Print "Base-1-Err"
Err.Clear
Resume CheckBase0
Base0Err:
Debug.Print "Base-0-Err"
Err.Clear
Resume ExitHere
End Sub
0 Likes
Message 4 of 4

Anonymous
Not applicable
>>at least I can quit thinking I'm losing my mind because of the inability
>>to
>>access item(0)
:-)
>>...so it must be due to some other cause ......!!


There's not much chance of keeping your sanity when dealing with vba 🙂

Jon
0 Likes