Martin said:
Hi all!
In VB6.0 A combobox had items, which basically were the description and
ItemData which could be used to create a link to a record, that is if the
recordset had a numeric key.
I was hoping that in VB2005 this would have been changed to an alphanumeric
itemdata, so that no longer need to create an array "on the side" to store
the key values. But I can't even find the itemdata anymore.
How do you store a key for each item in a combobox?
Tia,
Martin
Martin,
The really simplest way is to use the little-known
Microsoft.VisualBasic.Compatibility namespace (Project>Add
Reference>.NET Tab then highlight "Microsoft.VisualBasic.Compatibility"
and select OK)
You will now have several new functions, but the two you want are -
Microsoft.VisualBasic.Compatibility.VB6.SetItemData
+
Microsoft.VisualBasic.Compatibility.VB6.GetItemData
To use (ListBox or ComboBox) -
iNewIndex = ListBox1.Items.Add(sWhatever) 'String to Display
Microsoft.VisualBasic.Compatibility.VB6.SetItemData(ListBox1, iNewIndex,
iKeyCounter) 'iKeyCounter is a unique Index/Key integer
When the User clicks on your ListBox/ComboBox -
iKey = Microsoft.VisualBasic.Compatibility.VB6.GetItemData(ListBox1,
ListBox1.SelectedIndex)
You will now have the same functionality as you did in VB6!
To the .NET purists out there (if there is such a creature), I realise
you will probably be frowning on this approach, however before I used it
myself I applied my personal 4-point criteria -
1. PERFORMANCE: My tests have shown no measurable performance-hit in
using these functions when compared to Class or DataObject alternatives.
2. EASE OF USE: It is very simple to add the namespace and start using
the new functions.
3. READABILITY AND UNDERSTANDING: As these functions draw on knowledge
already obtained from VB6, I find it very easy to read and interpret
within my code.
4. LEGITIMACY: These functions are not derived from some "back-door"
method or even API implementation, they are provided as Functions by
Microsoft.
Enjoy!
ShaneO
There are 10 kinds of people - Those who understand Binary and those who
don't.