[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 488
  • Last Modified:

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.
 
0
misha1
Asked:
misha1
  • 3
  • 2
  • 2
  • +3
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
Richie_SimonettiIT OperationsCommented:
too late.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Richie_SimonettiIT OperationsCommented:
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
 
rkot2000Commented:
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
 
amebaCommented:
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
 
lschueleCommented:
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
 
misha1Author Commented:
Thanks guys,
 This suggestion by emoreau worked fine.
0
 
Éric MoreauSenior .Net ConsultantCommented:
if you have many items into your lists, ameba comments can make a big difference in performance.
0
 
misha1Author Commented:

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now