BUG: Resetting ListIndex Property Generates Click Event

Last reviewed: June 21, 1995
Article ID: Q79241
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0

SYMPTOMS

Resetting the ListIndex property of a list box, combo box, directory list box, or a file list box at run time generates a Click event for the control. For a drive list box, resetting the ListIndex property generates a Change event.

CAUSE

This is a result of the Windows subclass definition for these controls. When an item in one of these list boxes is selected, a Click event (or Change event for drive list box) occurs and the ListIndex property is updated. Conversely, when the ListIndex property is changed, a message occurs, generating the corresponding event.

WORKAROUND

Use the KeyUp procedure instead of click, and then call KeyUp when a key is pressed.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. Microsoft is researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

This behavior is not documented in the Visual Basic documentation or online Help. This behavior can cause some unexpected results. For example, if code in a Click (or Change) event procedure is assigning the selected items in the list box to an array (or directly to the Text property of another control), resetting the ListIndex property causes another such assignment, but with the new item.

If the ListIndex is reset to -1, a null item is assigned by the code because that setting indicates no item is selected.

Steps to Reproduce Problem

  1. Start Visual Basic or from the File menu, choose New Project if Visual Basic is already running. Form1 is created by default.

  2. Add a combo box (Combo1) to Form1.

  3. Add a text box (Text1) to Form1.

  4. Add a command button (Command1) to Form1.

  5. Add the following code to the Click event for the list box chosen:

       Sub Combo1_Click ()
          text1.text = combo1.text
       End Sub
    
       Note that for drive and directory list boxes, change the assignment to:
    
          text1.text=drive1.list(drive1.ListIndex)
    
             -or-
    
          text1.text=dir1.list(dir1.ListIndex)
    
    

  6. Add the following code to the Click event procedure for Command1:

       Sub Command1_Click ()
          combo1.ListIndex = -1
       End Sub
    
    

  7. Add the following code to the Form_Load event procedure of Form1:

       Sub Form_Load ()
          For n = 1 To 10
             combo1.AddItem Format$(n, "0")
          Next
       End Sub
    
    

  8. Run the program. Notice that when you click the Command1 button, the list box is updated as expected, the code in the Click event procedure for the list box is executed, and the Text property of the text box is changed.

REFERENCES

"Programming Windows: the Microsoft Guide to Writing Applications for Windows 3," by Charles Petzold, Microsoft Press, 1990

"Microsoft Windows Software Development Kit: Reference Volume 1," version 3.0

WINSDK.HLP file shipped with Microsoft Windows 3.0 Software Development Kit


Additional reference words: buglist1.00 buglist2.00 buglist3.00 1.00 2.00
3.00
KBCategory: kbprg kbbuglist
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.