My listview has 3 columns. "Text" , "Delay" and "".
The "" column is to show which one is being ran by filling it with a "<".

If I use
List.Items(currentTimer - 1).SubItems.Add("<")
Then it works, but how will I remove it?
SubItems(1) modifies column 1, and SubItems(2) is out of range.

Thanks in advance.

Dani AI

Generated

Short diagnosis: the error comes from using a count as an index. The SubItems collection is zero‑based and SubItems.Count returns the number of subitems — so the last valid index is Count - 1. Also note that SubItems(0) corresponds to the first column (the same text as the ListViewItem.Text), SubItems(1) is column 2, SubItems(2) is column 3, etc. and were correct about zero‑based indexing; ’s note about missing columns is also relevant — if an item doesn’t yet have a third subitem, trying to address SubItems(2) will throw that “index” error.

Two practical fixes:

  • Create items with the full set of subitems up front so you never hit a missing index.
  • Or ensure the target item has enough subitems before you assign to a column (add empty subitems until the collection is large enough).

Example helper (VB) to make access safe:

' Ensure an item has at least requiredCount subitems
Private Sub EnsureSubItems(item As ListViewItem, requiredCount As Integer)
    While item.SubItems.Count < requiredCount
        item.SubItems.Add(String.Empty)
    End While
End Sub

' Usage: safely set the third column (index 2) for an item
Dim itemIndex = currentTimer - 1
If itemIndex >= 0 AndAlso itemIndex < myList.Items.Count Then
    Dim li = myList.Items(itemIndex)
    EnsureSubItems(li, 3)
    li.SubItems(2).Text = "<"
End If

Quick troubleshooting tips: log both indices (itemIndex and the SubItems.Count) to see which is invalid (as suggested); confirm the ListView is in Details view; TabIndex is unrelated; instead of removing subitems prefer clearing the cell text or track the previous index and clear that item’s third column before setting the next one. This approach prevents the out‑of‑range exception and keeps the indicator logic reliable.

Recommended Answers

All 11 Replies

ListViewItems are zero based.

Therefore 0 will be column one, and 1 will be column two.

Sorry, I meant that.
0 = Column 1
1 = Column 2
2 = Out of range (Its suppost to be column 3)

did you already inserted an item to other coulumns?
I guess List.Items(currentTimer - 1).SubItems.Add("<") were added to column1.. only a guess :)

List.Items(currentTimer - 1).SubItems.Add("<") adds < to column 3.
I'm pretty sure thats only because column 1 and 2 are filled.

Just to clear this up a little bit:

SubItems(1) modifies column 2.
SubItems(2) is out of range. (It is suppost to be column 3.)
Items(int) modifies the first column.

There is obviously something wrong here.

Edit: In Microsoft Visual Studio, the properties for ListView, does "TabIndex" have something to do with this?
It's currently set to 0.

Can you please post the block of code that is throwing the error?

Seeing the code would help bring a better understanding of your problem.

 List.Items(currentTimer - 1).SubItems(List.Items(currentTimer - 1).SubItems.Count).Text = ""

Here is a peice of code that I expect to work.
But, there is an error.
"Value of '2' is not valid for 'index' "

Which index is it complaining about? You have two

  • currentTimer - 1
  • List.Items(currentTimer - 1).SubItems.Count

Try replacing the code with

Debug.WriteLine("index 1 = " & (CurrentTimes - 1).ToString)
Debug.WriteLine("index 2 = " & (List.Items(currentTimer - 1).SubItems.Count).ToString)
Debug.WriteLine("# items = " & List.Items.Count)
List.Items(currentTimer - 1).SubItems(List.Items(currentTimer - 1).SubItems.Count).Text = ""

The obvious problem is that you are either indexing past the number of items or past the number of subitems. You can't determine that just by looking at the code. You have to see the actual values of the indices.

The problem is in the second index, List.Items(currentTimer - 1).SubItems.Count .

My program waits the amount of time in column 2 (it has to be a double)
and then it prints what is in column 1. In column 3, there is suppost to be "<" indicating what it is waiting for.

List.Items(currentTimer - 1).SubItems(List.Items(currentTimer - 1).SubItems.Count).Text = ""

Will probably not work because Count will always return a number whic is one more than the highest valid index. Iitems and SubItems are zero-relative indexed so if you have 5 SubItems then then Count = 5 and the rightmost SubIitem has an index of 4.

commented: Sorry for absence. +8

As Jim has stated, listviews are zero based.

Therefore:

List.Items(currentTimer - 1).SubItems(List.Items(currentTimer - 1).SubItems.Count).Text = ""

Should be:

List.Items(currentTimer - 1).SubItems(List.Items(currentTimer - 1).SubItems.Count - 1).Text = ""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.