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?



try this following code :

Private Sub Form_Load()
Dim i As Integer
For i = 0 To 10
    List1.AddItem (i)
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
        End If
    End If
End Sub

Hope this helps...

commented: excellent +1
commented: nice one :) +1
commented: sings good +1

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 :)

commented: Good +1
commented: Always Help :P +1
commented: nice one +1

Exactly what I was looking for. Thanks much!

you're welcome my friend.
don't forget to mark this thread solved :)

commented: cookies +1
commented: thanks again.... +1

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
List1.Visible = True

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