Solved

# check all boxes in a ListBox and uncheck all.

Posted on 2003-03-31
Medium Priority
439 Views
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
Question by:ivc1883
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 3
• +2

LVL 10

Accepted Solution

aeklund earned 200 total points
ID: 8241290
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

LVL 10

Expert Comment

ID: 8241301
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

Expert Comment

ID: 8241307
'*********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

Expert Comment

ID: 8241486
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

LVL 10

Expert Comment

ID: 8241773
How annoying!  Why do ppl repost the same comments that are already posted?
0

LVL 6

Expert Comment

ID: 8241939
aeklund

Nice code.  Adding it to my API collection.
0

Author Comment

ID: 8242559
quick answer, helps a lot thanks.....
0

Expert Comment

ID: 8243248
Before I posted mine, there wasn't any answers
0

LVL 10

Expert Comment

ID: 8246280
ivan-

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

Expert Comment

ID: 8246702
TO: aeklund
Ok I will do that next time
0

Expert Comment

ID: 8255072
I apologize, when I started to post my comment nothing was there.
0

Expert Comment

ID: 8255120
I apologize, when I started to post my comment nothing was there.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code oâ€¦
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone â€¦
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can launâ€¦
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that aâ€¦
###### Suggested Courses
Course of the Month12 days, 12 hours left to enroll