 |
fabulous Level: VB Guru

 Registered: 03-08-2002 Posts: 439
|
Have your Own ItemData in .NET ComboBox
If you are like me, you would have relied on the Combobox's .ItemData property in VB.OLD for any of your lookup fields in a database app. Recently I was working on a project in .NET in a situation where I would normally have used the .ItemData property to get additional info on my list items.
I was disappointed in finding out that .NET's combobox doesn't support the .ItemData property. Not wanting to change the design of my application (since I am still learning .NET) I tried to come up with a way to have metadata about my list item.
I considered many things , some of which are totally brain-dead - like maintaining a form level collection indexed by the combobox item text and the data being the numeric index for the entry.
I also thought of making my own combobox with itemdata and while following this route, I noticed in the documentation that the .Add method for the combobox takes an object parameter. Reading between the lines reveals that everything in .NET inherits from System.Object and displaying it as text implicitly calls its .ToString() method whch by default returns its type.
When I realised this, I tried creating a structure with all the fields my lookup enty had in the database and tried adding it to the combobox and it worked. Which is nice. Following is some simple code showing how to implement this in both VB.NET and C# to show how to use this strategy to store details about Account Types. To keep it simple, we will store only the typeID, the Typename and the interest rate.
C#
//code for the stucture here
private struct AccountType
{
public int AccTypeID;
public string AccountName;
public double InterestRate;
public AccountType (int _AccTypeID, string _AccountName, double _InterestRate)
{
AccTypeID = _AccTypeID;
AccountName = _AccountName;
InterestRate = _InterestRate;
}
public override string ToString()
{
return this.AccountName;
}
} |
this is how you load it into the combo box and read it when the user clicks on one of them.
private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.Items.Add(new AccountType(23, "Fabulous Fadz Super Account", .9));
comboBox1.Items.Add(new AccountType(51, "A silly account that doesn't pay", .01));
comboBox1.Items.Add(new AccountType(84, "Yet another silly account", .02));
MessageBox.Show("The items are now loaded");
this.Text = "Select The Different Items and Watch";
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//show the data in the relevant fields
AccountType acc = (AccountType)comboBox1.SelectedItem;
textBox1.Text = acc.AccTypeID.ToString();
textBox2.Text = acc.AccountName;
textBox3.Text = ((int)(acc.InterestRate * 100)).ToString() + " %";
} |
VB.NET
This is the code for the structure.
Private Structure AccountType
Dim TypeID As Integer
Dim AccountTypeName As String
Dim InterestRate As Single
Public Sub New(ByVal _TypeID As Integer, ByVal _AccountTypeName As String, ByVal _InterestRate As Single)
'store these values
TypeID = _TypeID
AccountTypeName = _AccountTypeName
InterestRate = _InterestRate
End Sub
Public Overrides Function ToString() As String
Return Me.AccountTypeName
End Function
End Structure |
And the code to put this in the combobox and translate it when the user selects an item from the list.
Private Sub frmDemo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'for the purposes of this demo I am hard coding the entries
With cboAccounts
.Items.Add(New AccountType(23, "Fabulous Fadz Super Account", 0.9))
.Items.Add(New AccountType(12, "Not so fabulous dull account", 0.02))
.Items.Add(New AccountType(84, "Yet another silly account", 0.01))
End With
MessageBox.Show("The data is now loaded")
Me.Text = "Select different entries and watch"
End Sub
Private Sub cboAccounts_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboAccounts.SelectedIndexChanged
'in this case simply put the data up into the available fields
Dim acc As AccountType = CType(cboAccounts.SelectedItem, AccountType)
txtID.Text = acc.TypeID
txtAccName.Text = acc.AccountTypeName
txtInterest.Text = (acc.InterestRate * 100).ToString & " %"
End Sub |
I will develop the idea further or ditch it if I find a more efficient way. Just thouht I would share it with anyone who needed it. Feel free to ask any questions or make comments.
____________________________
My boss is a Jewish Carpenter (Jesus Christ)

Brain Bench Certified VB.NET Developer
|
|
03-01-2004 at 12:16 AM |
|
|
erhm Level: Trainee
 Registered: 23-02-2005 Posts: 1
|
Re: Have your Own ItemData in .NET ComboBox
Ditto. Fabulous baby!
I came very close in my own efforts. But used the "Shadows" keyword rather than "Overrides".
I've been coding for 20+ years in any number of languages. I'm finding the transition to .NET particulalry painful. Thanks!
|
|
23-02-2005 at 04:15 PM |
|
|
SBendBuckeye Level: Trainee
 Registered: 03-06-2005 Posts: 1
|
Re: Have your Own ItemData in .NET ComboBox
Very nice. I have been looking for a way to override the Items.Add for a custom combobox. This may help that process, but the problem I am having is trapping the standard Items.Add("One Value") which goes directly to MyBase.Item.Add.
Because the Items collection is a ComboBox.ObjectCollection I don't know how to override it. Does this pertain to that? Sorry for my lack of knowledge. I am coding in VB.Net. Thanks!
____________________________
Have a great day!
j2associates_NO_SPAM_@yahoo.com
|
|
03-06-2005 at 09:57 PM |
|
|
sergioseva Level: Trainee
 Registered: 28-03-2006 Posts: 1
|
Re: Have your Own ItemData in .NET ComboBox
I maked a solution in the same way that you, but i have a problem when i call the indexof method.
I'm explain using your example,
When i call cboAccounts.items.indexOf(new AccountType(12, "not so fabulous dull account", 0.02))
Should return 1 but return -1, then I try overriding the equals method of AccountType like this
public overrides function equals(byval obj as object) as boolean
if (obj.gettype is me.gettype) then
equals = (me.typeid = (AccountType)obj.typeid)
else
equals = false
end if
but it doesn´t work, the method doesn´t find the element
Do You know if is there a way to find the element without need to compare strings?
|
|
28-03-2006 at 08:30 PM |
|
|
deepsdeeps Level: Trainee
 Registered: 11-05-2006 Posts: 1
|
Re: Have your Own ItemData in .NET ComboBox
quote: fabulous wrote:
quote: When i call cboAccounts.items.indexOf(new AccountType(12, "not so fabulous dull account", 0.02))
What IndexOf does is look for the instance you gave it and tell you where it is. In this case, you are creating a new instance and searching for that and because of that reason, it cannot be found.
What you will need to do is to store the object somewhere first, and then later when you want it, you will search for the item as in...
'assuming acc was declared
acc = New AccountType(12, "not so fabulous dull account", 0.02))
cboAccounts.items.IndexOf(acc) |
Hope this helps
i tried the above code but it doesnt work pls mail me if any solution is there for takeing the index of a particular Itemdata
[Edited by deepsdeeps on 11-05-2006 at 11:35 AM GMT]
[Edited by deepsdeeps on 11-05-2006 at 11:36 AM GMT]
|
|
11-05-2006 at 11:34 AM |
|
|
|
|
 |
 |