471,066 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 471,066 developers and data experts.

Use inheritance to produce a really useful combo object

Cathode Follower
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:
Expand|Select|Wrap|Line Numbers
  1. Public Class ComboControl : Inherits System.Windows.Forms.ComboBox
  2.     Private _CodeDescriptions As New Dictionary(Of String, String)
  3.     Private _DescriptionCodes As New Dictionary(Of String, String)
  4.     Private _AllowNone As Boolean
  5.     Private _AllowAny As Boolean
  6.     Private _ContextFlag As String
  7.  
  8.     Public Sub Clear()
  9.         _CodeDescriptions.Clear()
  10.         _DescriptionCodes.Clear()
  11.         Me.Items.Clear()
  12.     End Sub
  13.  
  14.     Public Property AllowAny() As Boolean
  15.         Get
  16.             AllowAny = _AllowAny
  17.         End Get
  18.         Set(ByVal value As Boolean)
  19.             _AllowAny = value
  20.         End Set
  21.     End Property
  22.  
  23.     Public Property AllowNone() As Boolean
  24.         Get
  25.             AllowNone = _AllowNone
  26.         End Get
  27.         Set(ByVal value As Boolean)
  28.             _AllowNone = value
  29.         End Set
  30.     End Property
  31.  
  32.     Public Property ContextFlag() As String
  33.         Get
  34.             ContextFlag = _ContextFlag
  35.         End Get
  36.         Set(ByVal value As String)
  37.             _ContextFlag = value
  38.         End Set
  39.     End Property
  40.  
  41.     Public Sub Add(ByVal Code As String, ByVal Description As String)
  42.         If _CodeDescriptions.Count = 0 And AllowNone Then
  43.             Me.Items.Add("<None>")
  44.         End If
  45.  
  46.         If _CodeDescriptions.Count = 0 And AllowAny Then
  47.             Me.Items.Add("<Any>")
  48.         End If
  49.  
  50.         Me.Items.Add(Description)
  51.         _CodeDescriptions.Add(Code, Description)
  52.         If Not _DescriptionCodes.ContainsKey(Description) Then ' suppress duplicates
  53.             _DescriptionCodes.Add(Description, Code)
  54.         End If
  55.     End Sub
  56.  
  57.     Public Property Code() As String
  58.         Get
  59.             If Me.SelectedItem IsNot Nothing AndAlso Me.SelectedItem.ToString <> "" Then
  60.                 If _DescriptionCodes.ContainsKey(Me.SelectedItem.ToString) Then
  61.                     Return (_DescriptionCodes(Me.SelectedItem.ToString))
  62.                 Else
  63.                     Return ""
  64.                 End If
  65.             Else
  66.                 Return ""
  67.             End If
  68.         End Get
  69.         Set(ByVal value As String)
  70.             Dim SelectedItem As String = ""
  71.             If value <> "" Then
  72.                 If _CodeDescriptions.TryGetValue(value, SelectedItem) Then
  73.                     Me.SelectedItem = SelectedItem
  74.                 Else
  75.                     Add(value, "Unknown code " & value)
  76.                     Me.SelectedItem = value
  77.                 End If
  78.             End If
  79.         End Set
  80.     End Property
  81.  
  82.     Public Sub RemoveElement(ByVal Name As String)
  83.         Code = Name
  84.         Items.RemoveAt(SelectedIndex)
  85.         Dim Description = _CodeDescriptions(Name)
  86.         _CodeDescriptions.Remove(Name)
  87.         _DescriptionCodes.Remove(Description)
  88.     End Sub
  89.  
  90. End Class
  91.  
Loading the combo is easy. Here's an example:
Expand|Select|Wrap|Line Numbers
  1.       ccDefaultFrequencies.Clear()
  2.  
  3.         For Each objFrequency In objVisitFrequencies.Values
  4.             With objFrequency
  5.                 ccDefaultFrequencies.Add(.FrequencyId, .Description)
  6.             End With
  7.         Next
  8.  
Having loaded the combo, it is easy to set an appropriate value:
Expand|Select|Wrap|Line Numbers
  1. ccDefaultFrequencies.Code = "WEEK"
  2.  
- and picking up a selected value is correspondingly straightforward:
Expand|Select|Wrap|Line Numbers
  1. DefaultFrequency = ccDefaultFrequencies.Code
  2.  
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.
Feb 26 '09 #1
0 2397

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Joe Delphi | last post: by
14 posts views Thread by Steve Jorgensen | last post: by
12 posts views Thread by Steve Jorgensen | last post: by
7 posts views Thread by Hazz | last post: by
2 posts views Thread by Kevin Newman | last post: by
47 posts views Thread by Mark | last post: by
3 posts views Thread by Leo Seccia | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.