Microsoft KB Archive/145683

= BUG: Changing ListItem.Selected Doesn't Change SelectedItem =

Article ID: 145683

Article Last Modified on 11/18/2003

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q145683



SUMMARY
Setting the Selected property of a ListItem object in a ListView control to True does not change the SelectedItem property of the ListView control to correspond to this ListItem.



WORKAROUND
To avoid this problem, use the SelectedItem property of the ListView control instead of the Selected property of an individual ListItem. Setting the SelectedItem property of a ListView control to a given ListItem will correctly modify the Selected property of that ListItem.



STATUS
Microsoft has confirmed this to be an issue 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.



Steps to Reproduce
 Start Visual Basic, or if it is already running, choose New Project from the File menu. Add a single ListView control to Form1.  Add the following code to Form1: Option Explicit

Private Sub Form_Load Dim LI As ListItem Dim i As Integer

For i = 1 To 3 Set LI = listview1.ListItems.Add(,, "Test " & i)          LI.Selected = True Next i

Debug.Print "SelectedItem = " & listview1.SelectedItem.Text

For i = 1 To 3 Debug.Print listview1.ListItems(i).Text & ", Selected = " _ & listview1.ListItems(i).Selected Next i  End Sub

  Press F5 to run the project. Notice the output in the debug window:

SelectedItem = Test 1

Test 1, Selected = False

Test 2, Selected = False

Test 3, Selected = True

Although the Selected property of each ListItem is correct, the SelectedItem property of the ListView control itself still refers to the first ListItem. Setting the Selected property of the ListItems did not modify the SelectedItem property of the ListView control. To work around the problem, set the SelectedItem property instead. For example, in this sample, change the Selected line inside the first For..Next loop from LI.Selected = True

to: Set ListView1.SelectedItem = LI

Running the project after this change has been made will cause the expected output (that is, the Text property of SelectedItem is now 'Test 3') to be displayed in the debug window. 

Additional query words: 4.00 vb4win vb432

Keywords: kbbug KB145683

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.