Disable some entries in Combo box
I am loading a combo box with entries from a data base and based on certain conditions, I would like to disable some of the entries in the combo box so that the user may not click on these options. How do I disable some of the entries in the combo box??
Re: [bold]Disable some entries in Combo box[/bold]
I think, you can't.
Entries in a combo or a listbox are accessed as cbo.List(n) which retrieves the item string. The only property of these items is if they are selected or not. This property is accessed as cbo.Selected(n).
An 'Enabled' property does not exist. You have to deal with that by software.
Re: Disable some entries in Combo box
Thank you for your response. In other words, I can't even change colours of certain entries in the combo box?? This is because I thought that if its not possible to disable some entries, then I might change the colour to white so that it may not be visible to the user.. Would you or anyone have other suggestions of how to work around this problem??
Quote:
Originally Posted by WoF
I think, you can't.
Entries in a combo or a listbox are accessed as cbo.List(n) which retrieves the item string. The only property of these items is if they are selected or not. This property is accessed as cbo.Selected(n).
An 'Enabled' property does not exist. You have to deal with that by software.
Re: [bold]Disable some entries in Combo box[/bold]
The problem is, you require a combo-box.
The ListView control from 'Microsoft Windows Common Controls 6.0' provides extensive item properties, but has no dropdown feature...
There are a couple of custom listbox controls available freely in the internet. Try a search with "custom combobox control"
Re: Disable some entries in Combo box
Just a quick simple thought...
Why not remove the items from the list...
Code:
Combo1.RemoveItem ([ItemIndex])
Just remember to go through the list backwards..
Gremmy..
Re: Disable some entries in Combo box
Quote:
Originally Posted by nthaby
I am loading a combo box with entries from a data base and based on certain conditions, I would like to disable some of the entries in the combo box so that the user may not click on these options. How do I disable some of the entries in the combo box??
this may help little bit more
Code:
'where c is ComboBox
option explicit
Dim CllLocked As New Collection
Const IsLocked As Boolean = True
Private Sub c_Click()
Dim indexCombo As Integer
Dim keyColl As String
indexCombo = c.ListIndex
keyColl = "key" & CStr(indexCombo)
If CllLocked.Item(keyColl) = IsLocked Then
MsgBox "Index is Locked"
End If
End Sub
Private Sub Form_Load()
dim i as integer
For i = 1 To 10
c.AddItem i
CllLocked.Add IIf(i Mod 2 = 0, IsLocked, Not IsLocked), "key" & CStr(i - 1)
Next
End Sub
Re: [bold]Disable some entries in Combo box[/bold]
Just use a FlexGrid. You can change the color of each cell!
Re: [bold]Disable some entries in Combo box[/bold]
Or you will have to write your own control (owner drawn control).
Why don't you just remove the items from the combobox if they are not required. You can add them again when you need them.
Re: [b]Disable some entries in Combo box[/b]
Thanks to everybody who have contributed, I will try out all your suggestions and let you know how if it worked..
Re: [b]Disable some entries in Combo box[/b]
Dear nthaby,
here i have make some changes in the code given by hensa22, so that the user can't access the disabled selection and index reset to the previous valid seletion, so that c.text propperty also not usable by the user in case of disable selection!;)
Code:
Option Explicit
Dim CllLocked As New Collection
Const IsLocked As Boolean = True
Dim lastIndex As Integer
Private Sub c_Click()
Dim indexCombo As Integer
Dim keyColl As String
indexCombo = c.ListIndex
keyColl = "key" & CStr(indexCombo)
If CllLocked.Item(keyColl) = IsLocked Then
MsgBox "Index is Locked"
c.ListIndex = lastIndex
Else
lastIndex = c.ListIndex
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 10
c.AddItem i
CllLocked.Add IIf(i Mod 2 = 0, IsLocked, Not IsLocked), "key" & CStr(i - 1)
Next
End Sub
Quote:
Originally Posted by nthaby
Thanks to everybody who have contributed, I will try out all your suggestions and let you know how if it worked..
And hensa22 , ur code was so good...thanks for sharing with us!