Hierarchical ListBox LastIndex does not update after a Collapse event

I just discovered the cause of an odd bug on many of my apps that I’m building under 18r1.1. If you expand a folder, add the children for that expansion, and then collapse the row, the listbox’s LastIndex does not decrement to the collapsed row count.

For example:

Hierarchical ListBox with 3 Folders
LastIndex = 2
Expand one of the Folders and add 10 child rows
LastIndex = 12
Collapse the Folder
LastIndex = 12

Nothing that I do can get the LastIndex back to 2.

Mac OS 10.12.6, 10.13.5, and Windows 10 at this point.

Anyone else?

there is nothing documented that indicates that it should…

LastIndex is the index of the last row added with AddRow/AddFolder or InsertRow
The value of this would be meaningless should any other operation occur after one of these.

[quote=391787:@Dave S]there is nothing documented that indicates that it should…

LastIndex is the index of the last row added with AddRow/AddFolder or InsertRow
The value of this would be meaningless should any other operation occur after one of these.[/quote]
Good point and that answer points out my deeper mistake. As soon as I read it, I realized that I should have been using “ListCount” not “LastIndex” and the only thing that I can think of is that I let autocomplete fool me with the wrong item :D.

Thanks, Dave.

Isn’t it ListIndex that you have to use ?

In this case, I’m trapping mouse clicks in the dead area of the listbox, so I need to know the listcount.

Care to explain what ypu mean by that ? A click after the last populated Row ?

Listcount is how many rows are in the listbox
ListIndex is the currently selected row… so you may actually want Listindex = -1

I’m not sure that ListIndex - 1 is useful for too much. But ListCount - 1 may be what the OP was expecting LastIndex to represent.

Edit: Just reread it and saw you said ListIndex = -1, where I first read it as ListIndex - 1.

[quote=391861:@Douglas Handy]I’m not sure that ListIndex - 1 is useful for too much. But ListCount - 1 may be what the OP was expecting LastIndex to represent.

Edit: Just reread it and saw you said ListIndex = -1, where I first read it as ListIndex - 1.[/quote]
Nope - I’m manually managing mouse clicks to gain consistency across all three platforms. You can have a ListBox that is 20 rows high, but only contains 8 rows/folders. I need to know WHERE the user clicked and the starting point is - was it in a populated row, or was it in the empty space? ListCount -1 is what I need.

Exactly that, and “ListIndex = -1” doesn’t get the specific info that I’m looking for.

Why ?

IMHO: this is exactly what you need. A click in a Listbox but not in the Heading or in a valid Row (an existing Row) returns -1. So

If Me.ListIndex = -1 Then
// Click outside of a Row
End If

Just check.

Edit: Read ListBox.ListIndex.

Breakfast was good (to think).

If your code in Click Event looks like:

If Y > (Listbox1.ListCount * Listbox1.RowHeight) Then TextArea1.Text = "" TextField1.Text = "" End If

You can either use:

If Listbox1.ListIndex = -1 Then TextArea1.Text = "" TextField1.Text = "" End If

Is-it clear now ?