Link to home
Start Free TrialLog in
Avatar of ivc1883
ivc1883

asked on

check all boxes in a ListBox and uncheck all.

hi experts, i'm working with a listbox with style of "1-checkbox" and i want to check all the items in the list with a simple click in the botton , and also uncheck all with another botton.  i'm using VB6

thanks
--Ivan V.--
ASKER CERTIFIED SOLUTION
Avatar of aeklund
aeklund

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aeklund
aeklund

Here is a much faster way using API

Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" _
   (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long

Private Const LB_SETSEL = &H185&

Private Sub Command1_Click()
  'DeSelect ALL
  Call SendMessage(List1.hwnd, LB_SETSEL, True, ByVal -1)
End Sub

Private Sub Command2_Click()
  'Select ALL
  Call SendMessage(List1.hwnd, LB_SETSEL, False, ByVal -1)
End Sub

'*********To check all items**********
Private Sub Command1_Click()
    For i = 0 To List1.ListCount - 1
        List1.Selected(i) = True
    Next i
End Sub

'*********To uncheck all items**********
Private Sub Command2_Click()
    For i = 0 To List1.ListCount - 1
        List1.Selected(i) = False
    Next i
End Sub


Hope this helps
In VB6 this is how I have been doing this.


Private Sub cmdSelectAll_Click()
    intTotalItems = List1.ListCount - 1
    For intCheckAll = 0 To intTotalItems
        List1.Selected(intCheckAll) = True
    Next intCheckAll
End Sub

Private Sub cmdUnCheckAll_Click()
    intTotalItems = List1.ListCount - 1
    For intUnCheckAll = 0 To intTotalItems
        List1.Selected(intUnCheckAll) = False
    Next intUnCheckAll
End Sub

I hope this is what you were looking for.

~Matt McCants
How annoying!  Why do ppl repost the same comments that are already posted?
aeklund

Nice code.  Adding it to my API collection.
Avatar of ivc1883

ASKER

quick answer, helps a lot thanks.....
Before I posted mine, there wasn't any answers
ivan-
Glad to help..

simon-
As good practice, I always refresh right before I post so I don't run into these situations.
  1st Post: 03/31/2003 01:08PM PST
  2nd Post: 03/31/2003 01:10PM PST
  Your Post: 03/31/2003 01:11PM PST (3 mins later)

TO: aeklund
Ok I will do that next time
I apologize, when I started to post my comment nothing was there.
I apologize, when I started to post my comment nothing was there.