misha1
asked on
Simple VB ListBox question
Hello guys,
Somehow this simple task doesn't work by me.
On the form I have two ListBoxes:
lbPos and lbCat.
So, I need two Click events:
If user clicks on one box it deselects the other one,
so to avoid items selected in both boxes.
Focus and selection are taken away.
Looked at HideSelection method, but it didn't work right.
I need to events:
Private Sub lbPos_Click()
........' deselect lbCat
End Sub
Private Sub lbCat_Click()
........' deselect lbPos
End Sub
Thanks.
Somehow this simple task doesn't work by me.
On the form I have two ListBoxes:
lbPos and lbCat.
So, I need two Click events:
If user clicks on one box it deselects the other one,
so to avoid items selected in both boxes.
Focus and selection are taken away.
Looked at HideSelection method, but it didn't work right.
I need to events:
Private Sub lbPos_Click()
........' deselect lbCat
End Sub
Private Sub lbCat_Click()
........' deselect lbPos
End Sub
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
too late.
Ah!, but with that code, item selected in click event is not true.
Private Sub List1_Click()
List1.Selected(List1.ListI ndex) = True
Dim i As Integer
For i = 0 To List2.ListCount - 1
List2.Selected(i) = False
Next i
End Sub
Private Sub List2_Click()
List2.Selected(List2.ListI ndex) = True
Dim i As Integer
For i = 0 To List1.ListCount - 1
List1.Selected(i) = False
Next i
End Sub
Private Sub List1_Click()
List1.Selected(List1.ListI
Dim i As Integer
For i = 0 To List2.ListCount - 1
List2.Selected(i) = False
Next i
End Sub
Private Sub List2_Click()
List2.Selected(List2.ListI
Dim i As Integer
For i = 0 To List1.ListCount - 1
List1.Selected(i) = False
Next i
End Sub
from http://www.vb2themax.com/Item.asp?PageID=CodeBank&ID=340
Select or deselect all the ListBox items in the specified range use newState = True to select, or False to deselect
it affects all items if the last two arguments are omitted
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 Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _
Any, dest As Any, ByVal numBytes As Long)
Const LB_SELITEMRANGE = &H19B
' Select or deselect all the ListBox items in the specified range
'
' use newState = True to select, or False to deselect
' it affects all items if the last two arguments are omitted
Sub ListBoxSelectRange (lb As ListBox, ByVal newState As Boolean, _
Optional firstItem As Long, Optional lastItem As Long = -1)
Dim lParam As Long
' account for omitted lastItem argument.
If lastItem < 0 Then lastItem = lb.ListCount - 1
' lParam must contain the first item's index in its low-order word
' and the last item's index in its high-order word.
' We need this method to avoid overflow.
CopyMemory lParam, firstItem, 2
CopyMemory ByVal VarPtr(lParam) + 2, lastItem, 2
SendMessage lb.hwnd, LB_SELITEMRANGE, newState, ByVal lParam
End Sub
Select or deselect all the ListBox items in the specified range use newState = True to select, or False to deselect
it affects all items if the last two arguments are omitted
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 Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _
Any, dest As Any, ByVal numBytes As Long)
Const LB_SELITEMRANGE = &H19B
' Select or deselect all the ListBox items in the specified range
'
' use newState = True to select, or False to deselect
' it affects all items if the last two arguments are omitted
Sub ListBoxSelectRange (lb As ListBox, ByVal newState As Boolean, _
Optional firstItem As Long, Optional lastItem As Long = -1)
Dim lParam As Long
' account for omitted lastItem argument.
If lastItem < 0 Then lastItem = lb.ListCount - 1
' lParam must contain the first item's index in its low-order word
' and the last item's index in its high-order word.
' We need this method to avoid overflow.
CopyMemory lParam, firstItem, 2
CopyMemory ByVal VarPtr(lParam) + 2, lastItem, 2
SendMessage lb.hwnd, LB_SELITEMRANGE, newState, ByVal lParam
End Sub
Option Explicit
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal _
hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const LB_SETSEL = &H185
Private Sub lbCat_Click()
SendMessageLong lbPos.hwnd, LB_SETSEL, False, -1
End Sub
Private Sub lbPos_Click()
SendMessageLong lbCat.hwnd, LB_SETSEL, False, -1
End Sub
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal _
hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const LB_SETSEL = &H185
Private Sub lbCat_Click()
SendMessageLong lbPos.hwnd, LB_SETSEL, False, -1
End Sub
Private Sub lbPos_Click()
SendMessageLong lbCat.hwnd, LB_SETSEL, False, -1
End Sub
Welcome to the wonderful world of cascading events...
Add a module level boolean to supress events in emoreau's code:
Private mbSuppressClick As Boolean
Private Sub Form_Load()
With lbCat
.AddItem "Cat 1"
.AddItem "Cat 2"
.AddItem "Cat 3"
.AddItem "Cat 4"
.AddItem "Cat 5"
End With
With lbPos
.AddItem "Pos 1"
.AddItem "Pos 2"
.AddItem "Pos 3"
.AddItem "Pos 4"
.AddItem "Pos 5"
End With
With List1
.AddItem "Cat 1"
.AddItem "Cat 2"
.AddItem "Cat 3"
.AddItem "Cat 4"
.AddItem "Cat 5"
End With
With List2
.AddItem "Pos 1"
.AddItem "Pos 2"
.AddItem "Pos 3"
.AddItem "Pos 4"
.AddItem "Pos 5"
End With
End Sub
Private Sub lbCat_Click()
Dim i As Integer
If mbSuppressClick = True Then Exit Sub
mbSuppressClick = True
For i = 0 To lbPos.ListCount - 1
lbPos.Selected(i) = False
Next i
mbSuppressClick = False
End Sub
Private Sub lbPos_Click()
Dim i As Integer
If mbSuppressClick = True Then Exit Sub
mbSuppressClick = True
For i = 0 To lbCat.ListCount - 1
lbCat.Selected(i) = False
Next i
mbSuppressClick = False
End Sub
Add a module level boolean to supress events in emoreau's code:
Private mbSuppressClick As Boolean
Private Sub Form_Load()
With lbCat
.AddItem "Cat 1"
.AddItem "Cat 2"
.AddItem "Cat 3"
.AddItem "Cat 4"
.AddItem "Cat 5"
End With
With lbPos
.AddItem "Pos 1"
.AddItem "Pos 2"
.AddItem "Pos 3"
.AddItem "Pos 4"
.AddItem "Pos 5"
End With
With List1
.AddItem "Cat 1"
.AddItem "Cat 2"
.AddItem "Cat 3"
.AddItem "Cat 4"
.AddItem "Cat 5"
End With
With List2
.AddItem "Pos 1"
.AddItem "Pos 2"
.AddItem "Pos 3"
.AddItem "Pos 4"
.AddItem "Pos 5"
End With
End Sub
Private Sub lbCat_Click()
Dim i As Integer
If mbSuppressClick = True Then Exit Sub
mbSuppressClick = True
For i = 0 To lbPos.ListCount - 1
lbPos.Selected(i) = False
Next i
mbSuppressClick = False
End Sub
Private Sub lbPos_Click()
Dim i As Integer
If mbSuppressClick = True Then Exit Sub
mbSuppressClick = True
For i = 0 To lbCat.ListCount - 1
lbCat.Selected(i) = False
Next i
mbSuppressClick = False
End Sub
ASKER
Thanks guys,
This suggestion by emoreau worked fine.
This suggestion by emoreau worked fine.
if you have many items into your lists, ameba comments can make a big difference in performance.
ASKER
"ameba" comments?
Heard of it, how do you do them?
Thanks.
Dim i As Integer
For i = 0 To List2.ListCount - 1
List2.Selected(i) = False
Next i
End Sub
and
Private Sub List2_Click()
Dim i As Integer
For i = 0 To List1.ListCount - 1
List1.Selected(i) = False
Next i
End Sub