473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,399 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 2510

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

Similar topics

3
by: Joe Delphi | last post by:
Does Visual Basic support multiple inheritance? That is one child class inheriting from more than one parent class. JD
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
3
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but...
7
by: Hazz | last post by:
Are there any good references/articles/books which provide clarity toward my insecurity still on deciding how to model a complex system? I still feel uncomfortable with my understanding, even...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.