Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

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.--
0
ivc1883
Asked:
ivc1883
  • 4
  • 3
  • 3
  • +2
1 Solution
 
aeklundCommented:
Here is a simple solution:

Private Sub Command1_Click()
  'Select All
  Dim i As Integer
  For i = 0 To List1.ListCount - 1
    List1.Selected(i) = True
  Next
End Sub

Private Sub Command2_Click()
  'DeSelect All
  Dim i As Integer
  For i = 0 To List1.ListCount - 1
    List1.Selected(i) = False
  Next
End Sub
0
 
aeklundCommented:
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

0
 
simon780Commented:
'*********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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
MattMcCantsCommented:
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
0
 
aeklundCommented:
How annoying!  Why do ppl repost the same comments that are already posted?
0
 
GPrentice00Commented:
aeklund

Nice code.  Adding it to my API collection.
0
 
ivc1883Author Commented:
quick answer, helps a lot thanks.....
0
 
simon780Commented:
Before I posted mine, there wasn't any answers
0
 
aeklundCommented:
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)

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now