Should adding or deleting rows fire the ListBox Change event?

According to the docs, a Listbox Change event will fire when:

  • When the Listbox is clicked to give it the focus
  • When an empty row in the ListBox is clicked
  • When a cell is clicked even if it already is selected and the column is not editable
  • When a row is clicked to change the selection
    Shouldn’t it also fire when we add or delete rows/folders via code? Also, those all seem to be more aligned with “GotFocus” than “Change” because they all are caused by a user’s clicking in the ListBox rather than a content or state change.

Am I just being dense today? I can’t think of a negative to that. Anyone else?

[quote=427032:@Tim Jones]According to the docs, a Listbox Change event will fire when:

  • When the Listbox is clicked to give it the focus
  • When an empty row in the ListBox is clicked
  • When a cell is clicked even if it already is selected and the column is not editable
  • When a row is clicked to change the selection
    Shouldn’t it also fire when we add or delete rows/folders via code? Also, those all seem to be more aligned with “GotFocus” than “Change” because they all are caused by a user’s clicking in the ListBox rather than a content or state change.

Am I just being dense today? I can’t think of a negative to that. Anyone else?[/quote]

maybe add in a boolean IsLoading and set it to true when adding the row and set it back to false when done and on the listbox on change event, add some code to check if Isloading is false then do whatever else do nothing

[quote=427032:@Tim Jones]According to the docs, a Listbox Change event will fire when:

  • When the Listbox is clicked to give it the focus
  • When an empty row in the ListBox is clicked
  • When a cell is clicked even if it already is selected and the column is not editable
  • When a row is clicked to change the selection
    Shouldn’t it also fire when we add or delete rows/folders via code? Also, those all seem to be more aligned with “GotFocus” than “Change” because they all are caused by a user’s clicking in the ListBox rather than a content or state change.

Am I just being dense today? I can’t think of a negative to that. Anyone else?[/quote]
The Change event is related to a change in the user’s selection as opposed to a change in the listbox contents. If you wanted something like that, you could certainly subclass the listbox, create a ContentsChanged event definition and then override the AddRow, RemoveRow, etc methods to raise the event and then call their supers like this:

Sub AddRow(txt() as String) Super.AddRow(txt) RaiseEvent ContentsChanged End Sub

Okay - Knowing that, I’ve added a “ListBox.ListIndex = -1” to my calls to AddRow or AddFolder and that has taken care of it.

Should the description in the docs be modified to more specifically call out that out:

so i add in this line before i am loading data into the listbox???

After I would say :wink:

why???

After, since you probably don’t want the Change event to fire until after you’ve added the new data.

One other thought. If you’re adding data and the user has already made selections, you may wish to record the current selections in an array and then reselect them after the added data. That would result in the same affect and result as my not above while retaining the user’s selections.

If you’re just appending rows then the users selections won’t get altered unless you actually code it to change.

That post was aimed at calling Listbox.ListIndex = -1. THAT is what would clear the user’s selections, but without it, the Change event doesn’t fire.

You can also fire the Change event without “resetting” the listbox by adding the following after the data has been added:

Listbox1.ListIndex = Listbox1.LastIndex

I do that when I want the listbox to fire the Change event and remain on the last row that was entered (added) and NOT reset.