When you populate a combo you load it with data that normally comes in the form of a code and a description - but the combo is only interested in the description. By creating a class derived from a combo, with additional properties, you can use it to keep the codes as well. When the user selects a description, you can get the code from the control. Here's how I do it:
-
Public Class ComboControl : Inherits System.Windows.Forms.ComboBox
-
Private _CodeDescriptions As New Dictionary(Of String, String)
-
Private _DescriptionCodes As New Dictionary(Of String, String)
-
Private _AllowNone As Boolean
-
Private _AllowAny As Boolean
-
Private _ContextFlag As String
-
-
Public Sub Clear()
-
_CodeDescriptions.Clear()
-
_DescriptionCodes.Clear()
-
Me.Items.Clear()
-
End Sub
-
-
Public Property AllowAny() As Boolean
-
Get
-
AllowAny = _AllowAny
-
End Get
-
Set(ByVal value As Boolean)
-
_AllowAny = value
-
End Set
-
End Property
-
-
Public Property AllowNone() As Boolean
-
Get
-
AllowNone = _AllowNone
-
End Get
-
Set(ByVal value As Boolean)
-
_AllowNone = value
-
End Set
-
End Property
-
-
Public Property ContextFlag() As String
-
Get
-
ContextFlag = _ContextFlag
-
End Get
-
Set(ByVal value As String)
-
_ContextFlag = value
-
End Set
-
End Property
-
-
Public Sub Add(ByVal Code As String, ByVal Description As String)
-
If _CodeDescriptions.Count = 0 And AllowNone Then
-
Me.Items.Add("<None>")
-
End If
-
-
If _CodeDescriptions.Count = 0 And AllowAny Then
-
Me.Items.Add("<Any>")
-
End If
-
-
Me.Items.Add(Description)
-
_CodeDescriptions.Add(Code, Description)
-
If Not _DescriptionCodes.ContainsKey(Description) Then ' suppress duplicates
-
_DescriptionCodes.Add(Description, Code)
-
End If
-
End Sub
-
-
Public Property Code() As String
-
Get
-
If Me.SelectedItem IsNot Nothing AndAlso Me.SelectedItem.ToString <> "" Then
-
If _DescriptionCodes.ContainsKey(Me.SelectedItem.ToString) Then
-
Return (_DescriptionCodes(Me.SelectedItem.ToString))
-
Else
-
Return ""
-
End If
-
Else
-
Return ""
-
End If
-
End Get
-
Set(ByVal value As String)
-
Dim SelectedItem As String = ""
-
If value <> "" Then
-
If _CodeDescriptions.TryGetValue(value, SelectedItem) Then
-
Me.SelectedItem = SelectedItem
-
Else
-
Add(value, "Unknown code " & value)
-
Me.SelectedItem = value
-
End If
-
End If
-
End Set
-
End Property
-
-
Public Sub RemoveElement(ByVal Name As String)
-
Code = Name
-
Items.RemoveAt(SelectedIndex)
-
Dim Description = _CodeDescriptions(Name)
-
_CodeDescriptions.Remove(Name)
-
_DescriptionCodes.Remove(Description)
-
End Sub
-
-
End Class
-
Loading the combo is easy. Here's an example:
-
ccDefaultFrequencies.Clear()
-
-
For Each objFrequency In objVisitFrequencies.Values
-
With objFrequency
-
ccDefaultFrequencies.Add(.FrequencyId, .Description)
-
End With
-
Next
-
Having loaded the combo, it is easy to set an appropriate value:
-
ccDefaultFrequencies.Code = "WEEK"
-
- and picking up a selected value is correspondingly straightforward:
-
DefaultFrequency = ccDefaultFrequencies.Code
-
AllowAny and AllowNone permit the user to select either Any or None (but not both, they are mutually exclusive. Both return an empty string for the code.