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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling items in a ListBox with Checkbox items.

Status
Not open for further replies.

robdon

Programmer
May 21, 2001
252
0
0
ES
Hi,

I have a ListBox with the Style probperty set to CheckBoxes.

Is it possible to 'disable' certain items, so that only some of the checkboxes are enabled?

Thanks,

Rob D.
 
I think you can do this with the ListView control but I don't know of a way to do it with the standard ListBox
 
There may be a roundabout way to “disable” the checkbox within the ListBox. When you identify which item in the ListBox needs to be disabled, set the ItemData property for that item to a specific value:

' Disable a specific ListBox item.
Dim intIdx As Integer
For intIdx = 0 To lstDisable.ListCount - 1
If lstDisable.List(intIdx) = "Blue" Then
lstDisable.ItemData(intIdx) = 1
End If
Next intIdx

Next, you can check for the disabled item in the ListBox ItemCheck event and don’t allow the client to select that item:

Private Sub lstDisable_ItemCheck(Item As Integer)
' Check for disabled item and don't allow client
' to select it.
If lstDisable.ItemData(Item) = 1 Then
lstDisable.Selected(Item) = False
Exit Sub
End If

' Normal select process here, if necessary.
End Sub

What happens with the above code is, the client selects the item by clicking on the checkbox inside the ListBox. However, nothing happens, i.e. it remains unchecked.

If you need to make a disabled item “enabled” again, reset the ItemData property:

' Enable a disabled ListBox item.
Dim intIdx As Integer
For intIdx = 0 To lstDisable.ListCount - 1
If lstDisable.ItemData(intIdx) = 1 Then
lstDisable.ItemData(intIdx) = 0
End If
Next intIdx

This isn’t the cleanest way, especially for the client because they may call and ask why they can’t select a particular item.

Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top