I am programming in vb6 and am trying to cause ctrl-a to "select all" in a list box, but am not having much luck. Does anyone have any ideas on how this might be accomplished?

Thanks,

theory

try this following code :

Private Sub Form_Load()
Dim i As Integer
For i = 0 To 10
    List1.AddItem (i)
Next
End Sub

Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim intIndex As Integer
    If Shift = 2 Then
        If KeyCode = vbKeyA Then
            For intIndex = 0 To List1.ListCount - 1
                List1.Selected(intIndex) = True
            Next
        End If
    End If
End Sub

Hope this helps...

Comments
excellent
nice one :)
sings good

alternatively if you are going to have an "Edit" menu set the "Select all" to be bound to 'Ctrl A' and place lines 1-6 of the above answer.

oh, and don't forget to set MultiSelect = simple or extend on listbox properties. so item can be selected more than one. ok :)

Comments
Good
Always Help :P
nice one

This is my first reply - so here goes!

In situations where you have a large amount of data in the control,
I would like to suggest that you change the control's VISIBLE property to FALSE
while this code is making the rows selected.

This has two effects:
1) it speeds up the actual operation of the code and
2) it hides a rather ugly scrolling-down-the-control visual effect.

To do this just add List1.Visible = False before the FOR statement, and
add List1.Visible = True after the NEXT statement: as follows:

List1.Visible = False
For intIndex = 0 To List1.ListCount - 1
List1.Selected(intIndex) = True
Next
List1.Visible = True

This can also help the speed of loading data into other controls, such as the Listview control.

This question has already been answered. Start a new discussion instead.