How can I deselect every selected item in a listbox which has multiple select set to advanced ..
Printable View
How can I deselect every selected item in a listbox which has multiple select set to advanced ..
Code:
For i = 0 to List1.ListCount -1
If List1.Selected(i) then
List1.Selected = False
End if
Next i
Quote:
Originally posted by jim mcnamara
Code:
For i = 0 to List1.ListCount -1
If List1.Selected(i) then
List1.Selected = False
End if
Next i
The code above is incorrect, you will recieve errors.
First, you must remove selected items backwards.
And second, you must tell which item is to be unselected.
Here is the correct way:
Code:Private Sub Command1_Click()
On Error Resume Next
Dim i As Integer
With List1
For i = .ListCount - 1 To 0 Step -1
If .Selected(i) Then
.Selected(i) = False
End If
Next
End With
End Sub
Matthew is right - you do have to proceed from the bottom up. Other than that, I don't understand the 'Second, ...' idea.
JIm...Matthew is being picky..
you forgot the (x) after selected
:D
after all he is the "master" LOL!
;)
This should be the best solution without loop throught the entire listitem. But I juz having some problem in the MakeDWord function. which 'coz not all the item being deselected :(
Perhpas you guy can help :D
Code:Option Explicit
'//WIN32API Function
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
'//WIN32API Constant
Private Const LB_SELITEMRANGE = &H19B
Private Const LB_SELITEMRANGEEX = &H183
Private Sub Form_Load()
Dim idx As Long
For idx = 0 To 50
List1.AddItem "Item " & idx
Next
End Sub
Private Sub Command1_Click()
Dim ItmRange As Long
Let ItmRange = MakeDWord(0, 50)
SendMessage List1.hwnd, LB_SELITEMRANGE, 1, ItmRange
End Sub
Private Sub Command2_Click()
Dim ItmRange As Long
Let ItmRange = MakeDWord(0, 50)
SendMessage List1.hwnd, LB_SELITEMRANGE, 0, ItmRange
End Sub
Function MakeDWord(LoWord As Integer, HiWord As Integer) As Long
'//This function is generate the DWORD as require for the LB_SELITEMRANGE API constant
MakeDWord = (HiWord * &H10000) Or (LoWord And &HFFFF&)
End Function
Hey Chris.
I can't see anything wrong with the way you create the DWord. I have tested it 3 ways now, and it's fine. :)
But you are right, it doesn't work. I've tried to change the limits from 0 - 50 to 48 - 50, and it still just highlight element 18 to 50.
I'm sorry I can't help you anymore then this:(
BTW, why are you using:
Could you not just use:Code:Let ItmRange = MakeDWord(0, 50)
:confused: :confused: :confused:Code:ItmRange = MakeDWord(0, 50)
FOUND IT :D
Change the following
toCode:SendMessage List1.hwnd, LB_SELITEMRANGE, 1, ItmRange
And it works!!!!!!!!!!!Code:SendMessage List1.hwnd, LB_SELITEMRANGE, 1, byval ItmRange
wow.. it great! Doesn't realise the ByVal will 'coz the problem.
Anyway Thanks :)