On Sun, 19 Oct 2003 09:54:45 -0400, "Brad Allison"
<ku****@charter .net> wrote:
This is a newbie question.
I have a combo box in where the end user will select an item (an obedience
class - yes for dogs, not for developers) and then assign an obedience judge
to that class. When they save this record, I do not want that class in the
combo box to be enabled. Is there a way to 'disable' this selection only,
while keeping the other classes available to the user. And then on the flip
side, if the user deletes that obedience event record, this item will be
re-enabled in the list.
No. With the existing ComboBox you can only Remove items. I've knocked
up an "AdvancedComboB ox" that implements a "Disabled" collection.
Adding items to this collection will make the items unselectable and
grey them out. You will want to make some changes - particularly with
the behaviour. Cursor keys up and down the list should skip disabled
items. I've also been a bit lazy about the hilighting - Background
colour should be changed, and DrawBackground( ) should be called under
all circumstances.
--begin AdvancedComboBo x.vb--
Public Class AdvancedComboBo x
Inherits Windows.Forms.C omboBox
Private mDisabledItems As New ArrayList()
Private mPreviousIndex As Integer = -1
Public Sub New()
MyBase.New()
MyBase.DrawMode = System.Windows. Forms.DrawMode. OwnerDrawFixed
MyBase.DropDown Style = ComboBoxStyle.D ropDownList
End Sub
Public ReadOnly Property DisabledItems() As IList
Get
Return mDisabledItems
End Get
End Property
Private Sub AdvancedComboBo x_DrawItem(ByVa l sender As Object,
ByVal e As System.Windows. Forms.DrawItemE ventArgs) Handles
MyBase.DrawItem
If e.Index = -1 Or e.Index > MyBase.Items.Co unt - 1 Then
e.DrawBackgroun d()
Exit Sub
End If
Dim oTextBrush As Brush
If e.State And DrawItemState.S elected Then
If mDisabledItems. Contains(MyBase .Items.Item(e.I ndex))
Then
oTextBrush =
System.Drawing. SystemBrushes.I nactiveCaption
Else
e.DrawBackgroun d()
oTextBrush =
System.Drawing. SystemBrushes.H ighlightText
End If
Else
e.DrawBackgroun d()
If mDisabledItems. Contains(MyBase .Items.Item(e.I ndex))
Then
oTextBrush =
System.Drawing. SystemBrushes.I nactiveCaption
Else
oTextBrush = System.Drawing. SystemBrushes.C ontrolText
End If
End If
e.Graphics.Draw String(MyBase.I tems.Item(e.Ind ex), Me.Font,
oTextBrush, e.Bounds.X, e.Bounds.Y)
End Sub
Private Sub InitializeCompo nent()
MyBase.DrawMode = System.Windows. Forms.DrawMode. OwnerDrawFixed
MyBase.DropDown Style = ComboBoxStyle.D ropDownList
End Sub
Public Shadows ReadOnly Property DrawMode() As DrawMode
Get
Return MyBase.DrawMode
End Get
End Property
Public Shadows ReadOnly Property DropDownStyle() As ComboBoxStyle
Get
Return MyBase.DropDown Style
End Get
End Property
Protected Overrides Sub OnSelectedIndex Changed(ByVal e As
System.EventArg s)
If Me.SelectedInde x <> -1 Then
If
mDisabledItems. Contains(MyBase .Items.Item(Me. SelectedIndex)) Then
MyBase.Selected Index = mPreviousIndex
Else
mPreviousIndex = MyBase.Selected Index
End If
End If
End Sub
End Class
--end AdvancedComboBo x.vb--
--begin ExampleUsage.vb--
Dim oAdvancedComboB ox As New AdvancedComboBo x() ' Duh
oAdvancedComboB ox.Items.Add("O ne")
oAdvancedComboB ox.Items.Add("T wo")
oAdvancedComboB ox.Items.Add("T hree")
oAdvancedComboB ox.Items.Add("F our")
oAdvancedComboB ox.DisabledItem s.Add("Two")
--end ExampleUsage.vb--
Rgds,