Previous | Index | Next 

[PRB] Removing an item from a multi-selectable ListBox resets ListIndex property

Here’s an undocumented behavior of the VB6 ListBox control: if you remove the currently selected item with the RemoveItem method, the value that the ListIndex property assumes after the removal depends on the MultiSelect property:

  • if MultiSelect is equal to 0-None, then the ListIndex property is assigned the value -1 and no item appears to be selected in the listbox;
  • if the MultiSelect property is set to 1-Simple or 2-Extended, then the ListIndex property is assigned a value one minus the index of the element that was removed.

The following code snippet shows this feature:

        ' lstSimple has MultiSelect=0-None, lstMulti has MultiSelect=2-Extended
        lstSimple.ListIndex = 10
        lstSimple.RemoveItem 10
        Debug.Print lstSimple.ListIndex     ' displays -1
        lstMulti.ListIndex = 10
        lstMulti.RemoveItem 10
        Debug.Print lstMulti.ListIndex      ' displays 9

All .NET listbox controls – including the VB6ListBox and VB6WLList controls – behave in a more predictable way: if you remove the item that is currently selected then the ListIndex property (and the SelectedIndex property) are set equal to -1, regardless of whether the listbox control supports multiple selections.

In most cases this detail has no impact on how a migrated VB.NET application behaves. If it does, however, you may want to manually set the value of the ListIndex property after all calls to the Remove method:

        lstMulti.ListIndex = 10
        lstMulti.RemoveItem 10
        lstMulti.ListIndex = 9

 

Previous | Index | Next