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