Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Box - Check box feature 1

Status
Not open for further replies.

vbmorton

Technical User
Dec 27, 2004
44
US
Can someone give me some advice.

i currently have.

Private Sub load()
Dim i As Integer


lst.AddItem "1 "
lst.AddItem "6 "
lst.AddItem "7 "
lst.AddItem "8 "
lst.AddItem "9 "

lst.Selected(0) = True

End Sub


I only want to allow the user to select one of the items, it defaults to item 1, but if the user selects another item, i only want that other item selected not the one that was previously selected. I dont see a property that would allow for only one selection at a time.

is this possible.????


VBMorton
 
?

Set the:
style to "0 - standard", and
multiselect to "0 - none".

You have mistaken some things over here.
If it is a checklistbox you can have as many as you want items checked. Then we are talking about a checkeditems collection.
The item the has a blue (default) backcolor IS the selected item.

As i understood, you have an item checked and you want when you check an other, the previews checked to be un-checked. This is useless.
 
vbmorton, what I think tipgiver is trying to explain is that it isn't useful for Check boxes to support "one and only one selection" functionality, since it's supported by option buttons. Option buttons are intended to support one and only one selection. Check boxes are intended to support zero-to-many selections. Clearly, you're confusing the two. So, lose your checkboxes and replace them with option buttons.

A little more information on option buttons best practices:

1. Put them in a control array. That way, you'll have one click event for the whole set of buttons.
2. Put them in a frame. If you have more than one set, you HAVE to put them in a frame.
3. If you have more than one set of buttons, put each set as its own control array, and have it in its own frame.

If you need more help, post back.

HTH

Bob
 
Bob, I think what the OP is referring to is using the checkbox style of the listbox (not that there is a ton of info in the OP). Whilst your explanation has application to this issue there is no way to add the 'option button' to the listbox as you can the checkbox.

>>This is useless.
I would agree with the exception being aesthetics. Some application use the checkbox styles and if you need to make it look like something else or if your users prefer to see the checkbox, etc. If all you want is the functionality of only being able to select a single item, then you can drop off the checkboxes from the listbox and set the Multiselect property to none. (as TipGiver already suggested)
 
Oops, you're dead right bj. As you say also, the exception to tipgiver's abjuration is aesthetics.

So, vbmorton, here ya go:
Code:
Private Sub List1_Click()
Dim i As Integer
With List1
    For i = 0 To .ListCount - 1
        List1.Selected(i) = .ListIndex = i
    Next i
End With
End Sub
This also has the effect of allowing the user to click anywhere on the line item to check the box, rather than just in the checkbox itself.

If there is a very long list, it gets a bit slow, though. This is faster:
Code:
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 Const LB_GETSELITEMS = &H191

Private Sub list1_Click()
Dim SelIndices() As Long
Dim SelCt As Integer
Dim i As Integer
If List1.SelCount = 0 Then
    Exit Sub
End If
SelCt = List1.SelCount
ReDim SelIndices(SelCt - 1)
SendMessage List1.hwnd, LB_GETSELITEMS, SelCt, SelIndices(0)
With List1
    For i = 0 To UBound(SelIndices)
        If SelIndices(i) <> .ListIndex Then
            .Selected(SelIndices(i)) = False
        End If
    Next i
End With
End Sub
If you want to keep the behavior of selecting by clicking anywhere on the line, instead of just the check box, then substitute this:
Code:
List1.Selected(List1.ListIndex) = True
for this:
Code:
If List1.SelCount = 0 Then
    Exit Sub
End If
HTH

Bob
 
Only slow because of the looping. You can always avoid that by working with the ItemCheck event

Private Sub List1_ItemCheck(Item As Integer)
Static LastItem As Integer
If LastItem <> Item Then
List1.Selected(LastItem) = False
LastItem = Item
End If
End Sub
 
Well, I tried your code, strongm, and it runs significantly better than either of mine. Star for you! vbmorton, strongm's solution is clearly the way to go.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top