[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Simple VB ListBox question

Posted on 2002-04-04
10
Medium Priority
?
481 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
[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
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

650 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