Combo Box selection

  • Thread starter Thread starter Brad Allison
  • Start date Start date
B

Brad Allison

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.

Thanks for the information.

Brad
 
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 "AdvancedComboBox" 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 AdvancedComboBox.vb--
Public Class AdvancedComboBox
Inherits Windows.Forms.ComboBox

Private mDisabledItems As New ArrayList()
Private mPreviousIndex As Integer = -1

Public Sub New()
MyBase.New()
MyBase.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
MyBase.DropDownStyle = ComboBoxStyle.DropDownList
End Sub

Public ReadOnly Property DisabledItems() As IList
Get
Return mDisabledItems
End Get
End Property

Private Sub AdvancedComboBox_DrawItem(ByVal sender As Object,
ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles
MyBase.DrawItem
If e.Index = -1 Or e.Index > MyBase.Items.Count - 1 Then
e.DrawBackground()
Exit Sub
End If

Dim oTextBrush As Brush
If e.State And DrawItemState.Selected Then
If mDisabledItems.Contains(MyBase.Items.Item(e.Index))
Then
oTextBrush =
System.Drawing.SystemBrushes.InactiveCaption
Else
e.DrawBackground()
oTextBrush =
System.Drawing.SystemBrushes.HighlightText
End If
Else
e.DrawBackground()
If mDisabledItems.Contains(MyBase.Items.Item(e.Index))
Then
oTextBrush =
System.Drawing.SystemBrushes.InactiveCaption
Else
oTextBrush = System.Drawing.SystemBrushes.ControlText
End If
End If
e.Graphics.DrawString(MyBase.Items.Item(e.Index), Me.Font,
oTextBrush, e.Bounds.X, e.Bounds.Y)
End Sub

Private Sub InitializeComponent()
MyBase.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
MyBase.DropDownStyle = ComboBoxStyle.DropDownList
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.DropDownStyle
End Get
End Property

Protected Overrides Sub OnSelectedIndexChanged(ByVal e As
System.EventArgs)
If Me.SelectedIndex <> -1 Then
If
mDisabledItems.Contains(MyBase.Items.Item(Me.SelectedIndex)) Then
MyBase.SelectedIndex = mPreviousIndex
Else
mPreviousIndex = MyBase.SelectedIndex
End If
End If
End Sub
End Class
--end AdvancedComboBox.vb--

--begin ExampleUsage.vb--
Dim oAdvancedComboBox As New AdvancedComboBox() ' Duh
oAdvancedComboBox.Items.Add("One")
oAdvancedComboBox.Items.Add("Two")
oAdvancedComboBox.Items.Add("Three")
oAdvancedComboBox.Items.Add("Four")

oAdvancedComboBox.DisabledItems.Add("Two")
--end ExampleUsage.vb--


Rgds,
 
Back
Top