[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Simple VB ListBox question

Posted on 2002-04-04
10
Medium Priority
?
487 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 70

Accepted Solution

by:
Éric Moreau earned 200 total points
ID: 6919200
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
ID: 6919203
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
ID: 6919205
too late.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6919215
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
ID: 6919223
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
 
LVL 15

Expert Comment

by:ameba
ID: 6919279
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
ID: 6919352
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
ID: 6919465
Thanks guys,
 This suggestion by emoreau worked fine.
0
 
LVL 70

Expert Comment

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

Author Comment

by:misha1
ID: 6925573

 "ameba" comments?
 Heard of it, how do you do them?
 
 Thanks.
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!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 Month19 days, 17 hours left to enroll

872 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