• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

Disable options in a list box!!

Hey experts,

I have a list box with a series of options but i want to be able to disable some of these options as they are not available, hence stopping the user selecting this option...how do i do this!

Thanks in advance people
Matt
0
MattAllen
Asked:
MattAllen
1 Solution
 
TimCotteeCommented:
Hi MattAllen,

Without subclassing it and handling that bit yourself, you cannot. However you can simulate this kind of behaviour

Private Sub lstMyList_Click()
    If lstMyList.ItemData(lstMyList.ListIndex) = 0 Then 'using .ItemData property to determine whether it is selectable or not
        msgbox "Cannot select this option it is unavailable"
        lstMyList.ListIndex = -1 'Deselect item
    End If
End Sub

Tim Cottee
0
 
fds_fatboyCommented:
You might be able to use a listview instead of a listbox. You can simulate disabling an item by changing its forecolor property, then catching it in the itemcheck event handler.

Here is an example.

Open a New VB Standard Exe Application. In Form1 add a listview control (ListView1) and add the following code to Form1:

Option Explicit

Private Sub Form_Load()
    Dim i As Long
   
    ListView1.View = lvwList
    ListView1.Checkboxes = True
    ListView1.LabelEdit = lvwManual

    For i = 0 To 19
        ListView1.ListItems.Add , , "Item " & i
    Next
   
    ListView1.ListItems(3).ForeColor = vbGrayText 'Disable an item
End Sub

Private Sub ListView1_ItemCheck(ByVal Item As MSComctlLib.ListItem)
    If Item.ForeColor = vbGrayText Then 'If disabled...
        Item.Checked = False            '... Reset checkbox
    End If
End Sub
0
 
MattAllenAuthor Commented:
Cheers fellas,

i needed a msgbox to come up ideally to tell the user becasue it is only a demo version of the software but thanks a lot guys

Matt
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now