Solved

Simple VB ListBox question

Posted on 2002-04-04
10
448 Views
Last Modified: 2007-12-19
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.
 
0
Comment
Question by:misha1
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 50 total points
Comment Utility
something like this:

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
End Sub

Private Sub lbCat_Click()
Dim i As Integer

    For i = 0 To lbPos.ListCount - 1
        lbPos.Selected(i) = False
    Next i
End Sub

Private Sub lbPos_Click()
Dim i As Integer

    For i = 0 To lbCat.ListCount - 1
        lbCat.Selected(i) = False
    Next i
End Sub
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
Private Sub List1_Click()
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
too late.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
Ah!, but with that code, item selected in click event is not true.


Private Sub List1_Click()
List1.Selected(List1.ListIndex) = 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.ListIndex) = True
Dim i As Integer
For i = 0 To List1.ListCount - 1
    List1.Selected(i) = False
Next i
End Sub


0
 
LVL 5

Expert Comment

by:rkot2000
Comment Utility
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
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 15

Expert Comment

by:ameba
Comment Utility
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
0
 

Expert Comment

by:lschuele
Comment Utility
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
0
 
LVL 1

Author Comment

by:misha1
Comment Utility
Thanks guys,
 This suggestion by emoreau worked fine.
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
if you have many items into your lists, ameba comments can make a big difference in performance.
0
 
LVL 1

Author Comment

by:misha1
Comment Utility

 "ameba" comments?
 Heard of it, how do you do them?
 
 Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now