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:
[code=vbnet]
Public Class ComboControl : Inherits System.Windows. Forms.ComboBox
Private _CodeDescriptio ns As New Dictionary(Of String, String)
Private _DescriptionCod es As New Dictionary(Of String, String)
Private _AllowNone As Boolean
Private _AllowAny As Boolean
Private _ContextFlag As String
Public Sub Clear()
_CodeDescriptio ns.Clear()
_DescriptionCod es.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 _CodeDescriptio ns.Count = 0 And AllowNone Then
Me.Items.Add("< None>")
End If
If _CodeDescriptio ns.Count = 0 And AllowAny Then
Me.Items.Add("< Any>")
End If
Me.Items.Add(De scription)
_CodeDescriptio ns.Add(Code, Description)
If Not _DescriptionCod es.ContainsKey( Description) Then ' suppress duplicates
_DescriptionCod es.Add(Descript ion, Code)
End If
End Sub
Public Property Code() As String
Get
If Me.SelectedItem IsNot Nothing AndAlso Me.SelectedItem .ToString <> "" Then
If _DescriptionCod es.ContainsKey( Me.SelectedItem .ToString) Then
Return (_DescriptionCo des(Me.Selected Item.ToString))
Else
Return ""
End If
Else
Return ""
End If
End Get
Set(ByVal value As String)
Dim SelectedItem As String = ""
If value <> "" Then
If _CodeDescriptio ns.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(B yVal Name As String)
Code = Name
Items.RemoveAt( SelectedIndex)
Dim Description = _CodeDescriptio ns(Name)
_CodeDescriptio ns.Remove(Name)
_DescriptionCod es.Remove(Descr iption)
End Sub
End Class
[/code]
Loading the combo is easy. Here's an example:
[code=vbnet]
ccDefaultFreque ncies.Clear()
For Each objFrequency In objVisitFrequen cies.Values
With objFrequency
ccDefaultFreque ncies.Add(.Freq uencyId, .Description)
End With
Next
[/code]
Having loaded the combo, it is easy to set an appropriate value:
[code=vbnet]
ccDefaultFreque ncies.Code = "WEEK"
[/code] - and picking up a selected value is correspondingly straightforward :
[code=vbnet]
DefaultFrequenc y = ccDefaultFreque ncies.Code
[/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.
[code=vbnet]
Public Class ComboControl : Inherits System.Windows. Forms.ComboBox
Private _CodeDescriptio ns As New Dictionary(Of String, String)
Private _DescriptionCod es As New Dictionary(Of String, String)
Private _AllowNone As Boolean
Private _AllowAny As Boolean
Private _ContextFlag As String
Public Sub Clear()
_CodeDescriptio ns.Clear()
_DescriptionCod es.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 _CodeDescriptio ns.Count = 0 And AllowNone Then
Me.Items.Add("< None>")
End If
If _CodeDescriptio ns.Count = 0 And AllowAny Then
Me.Items.Add("< Any>")
End If
Me.Items.Add(De scription)
_CodeDescriptio ns.Add(Code, Description)
If Not _DescriptionCod es.ContainsKey( Description) Then ' suppress duplicates
_DescriptionCod es.Add(Descript ion, Code)
End If
End Sub
Public Property Code() As String
Get
If Me.SelectedItem IsNot Nothing AndAlso Me.SelectedItem .ToString <> "" Then
If _DescriptionCod es.ContainsKey( Me.SelectedItem .ToString) Then
Return (_DescriptionCo des(Me.Selected Item.ToString))
Else
Return ""
End If
Else
Return ""
End If
End Get
Set(ByVal value As String)
Dim SelectedItem As String = ""
If value <> "" Then
If _CodeDescriptio ns.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(B yVal Name As String)
Code = Name
Items.RemoveAt( SelectedIndex)
Dim Description = _CodeDescriptio ns(Name)
_CodeDescriptio ns.Remove(Name)
_DescriptionCod es.Remove(Descr iption)
End Sub
End Class
[/code]
Loading the combo is easy. Here's an example:
[code=vbnet]
ccDefaultFreque ncies.Clear()
For Each objFrequency In objVisitFrequen cies.Values
With objFrequency
ccDefaultFreque ncies.Add(.Freq uencyId, .Description)
End With
Next
[/code]
Having loaded the combo, it is easy to set an appropriate value:
[code=vbnet]
ccDefaultFreque ncies.Code = "WEEK"
[/code] - and picking up a selected value is correspondingly straightforward :
[code=vbnet]
DefaultFrequenc y = ccDefaultFreque ncies.Code
[/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.