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 "EnableItemComb oBox" 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 EnableItemCombo Box
Inherits ComboBox
Private vDisabledItems As New Collection
Public Property DisabledItems(B yVal Index As Integer) As Boolean
Get
Dim retVal As Boolean
If vDisabledItems. Contains("Key" & CStr(Index)) Then
retVal = CBool(vDisabled Items("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 EnableItemCombo Box_DrawItem(By Val sender As Object, ByVal e
As System.Windows. Forms.DrawItemE ventArgs) Handles Me.DrawItem
Dim Pn As Pen
If DisabledItems(e .Index) = False Then
Pn = New Pen(Color.FromK nownColor(Known Color.WindowTex t))
Else
Pn = New Pen(Color.FromK nownColor(Known Color.InactiveC aptionText))
End If
Dim Br As Brush = Pn.Brush
e.DrawBackgroun d()
e.DrawFocusRect angle()
e.Graphics.Draw String(Me.Items (e.Index).ToStr ing, e.Font, Br,
e.Bounds.X, e.Bounds.Y)
End Sub
Private Sub EnableItemCombo Box_SelectedInd exChanged(ByVal sender As
Object, ByVal e As System.EventArg s) Handles Me.SelectedInde xChanged
If DisabledItems(M e.SelectedIndex ) = True Then
Dim NewIndex As Integer = FindEnabledInde x(Me.SelectedIn dex, True)
If NewIndex = -1 Then
NewIndex = FindEnabledInde x(Me.SelectedIn dex, False)
End If
If NewIndex -1 Then
Me.SelectedInde x = NewIndex
Beep()
End If
End If
End Sub
Private Function FindEnabledInde x(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(B yVal sender As System.Object, ByVal e As _
System.EventArg s) Handles Button1.Click
EnableItemCombo Box1.Items.Add( "Enabled")
EnableItemCombo Box1.Items.Add( "Disabled")
EnableItemCombo Box1.DisabledIt ems(1) = True
EnableItemCombo Box1.DrawMode = DrawMode.OwnerD rawFixed
End Sub
End Class
On 02.08.2008 04:49, Kevinp wrote:
On Wed, 30 Jul 2008 12:10:27 -0400, Kevinp
<kmahoney@nospa m_fireacademy.o rg wrote:
>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.Item s(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?