Disable items in Combobox

  • Thread starter Thread starter Kevinp
  • Start date Start date
K

Kevinp

I've spent the last four hours Google searching for a way to disable
items in a Combobox. I found one example in C++ which I can't get to
work and another in C# that I couldn't get to work either.

Does anyone have some good code for an owner-drawn combobox?
 
This would be a tough one to hack. You can make a lineitem user
control that can be disabled at will and handles the selection code.
The line item held within another usercontrol that has some kind of
dynamic sizing to handle adding of multiple line items. Me personally
the best way I've found to lock down an item in a drop down is to
remove it.
 
I've spent the last four hours Google searching for a way to disable
items in a Combobox. I found one example in C++ which I can't get to
work and another in C# that I couldn't get to work either.

Does anyone have some good code for an owner-drawn combobox?


I've already got a custom combobox control that I copied from someone
else and made the changes that I needed.

What I can't figure out is how to add a property to an item in the
combobox.

Like this: cboMyCombo.Items(2).Enabled = False

I know how to make a property for the Combobox, but not for an item in
the Combobox. Can anyone help me?
 
What about a trick.. You can set on an Array what index want to "disable".
On the SelectedIndexChanged event you can check if that option is in the
Array and then do a SelectedIndex = 0 (or the last one selected)

Many stuff is just simply tricks (like this)

Hope this help
 
Hello Kevin,

the following code will have a quick hack version of what you need.
Just create a form and add the following code. Then, compile the project.

Now, add one "EnableItemComboBox" and a button. Copy the Form1 code into
that form.

Run the program and click the button. Then, open the list of the
ComboBox. The "Disabled" entry will be in the color your Windows
settings define for DisabledCaption (Default: light gray).

Best regards,

Martin

Public Class EnableItemComboBox
Inherits ComboBox

Private vDisabledItems As New Collection

Public Property DisabledItems(ByVal Index As Integer) As Boolean
Get
Dim retVal As Boolean
If vDisabledItems.Contains("Key" & CStr(Index)) Then
retVal = CBool(vDisabledItems("Key" & CStr(Index)))
Else
retVal = False
End If
Return retVal
End Get
Set(ByVal value As Boolean)
If vDisabledItems.Contains("Key" & CStr(Index)) Then
vDisabledItems.Remove("Key" & CStr(Index))
End If

If value = True Then
vDisabledItems.Add("-1", "Key" & CStr(Index))
End If
End Set
End Property

Private Sub EnableItemComboBox_DrawItem(ByVal sender As Object, ByVal e
As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
Dim Pn As Pen
If DisabledItems(e.Index) = False Then
Pn = New Pen(Color.FromKnownColor(KnownColor.WindowText))
Else
Pn = New Pen(Color.FromKnownColor(KnownColor.InactiveCaptionText))
End If

Dim Br As Brush = Pn.Brush
e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawString(Me.Items(e.Index).ToString, e.Font, Br,
e.Bounds.X, e.Bounds.Y)
End Sub

Private Sub EnableItemComboBox_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles Me.SelectedIndexChanged
If DisabledItems(Me.SelectedIndex) = True Then
Dim NewIndex As Integer = FindEnabledIndex(Me.SelectedIndex, True)
If NewIndex = -1 Then
NewIndex = FindEnabledIndex(Me.SelectedIndex, False)
End If

If NewIndex > -1 Then
Me.SelectedIndex = NewIndex
Beep()
End If
End If
End Sub

Private Function FindEnabledIndex(ByVal StartValue As Integer, ByVal
Descending As Boolean) As Integer
Dim EndValue As Integer
Dim Steps As Integer
Dim t As Integer
Dim retValue As Integer = -1

If Descending = True Then
StartValue = Items.Count - 1
EndValue = 0
Steps = -1
Else
StartValue = 0
EndValue = Items.Count - 1
Steps = 1
End If

For t = StartValue To EndValue Step Steps
If DisabledItems(t) = False Then
retValue = t
Exit For
End If
Next

Return retValue
End Function
End Class



Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
EnableItemComboBox1.Items.Add("Enabled")
EnableItemComboBox1.Items.Add("Disabled")
EnableItemComboBox1.DisabledItems(1) = True
EnableItemComboBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub
End Class
 
Thanks Martin. Exactly what I needed. I did make one change though. I
added this to the class:

Public Sub New()
Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
End Sub

....instead of adding it to every form I use the new control on. No
biggee--just saves a little work.

Thanks,
Kevin
 
Back
Top