Combobox in VB2005

M

Martin

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
 
A

Armin Zingler

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?


You can store whole objects now in a combobox. You are not limited to
text+itemdata anymore. The object's ToString method returns the text to be
displayed in the combobox. If you can not override ToString or if it does
not return the text to be displayed in the combo, write a wrapper class:

class comboitem
public readoly item as MyObjectType
public sub new(byval item as MyObjectType)
me.item = item
end sub

public overrides function ToString() as string
return item.whateverYouWant
end function
end class


Add item to the combo:

cbo.items.add(new comboitem(yourObject))



Armin
 
C

Cor Ligthert [MVP]

Martin,

The most simple way is to create a datatable for that
\\\
dim dt as new datatable
dt.columns.add("Names")
dt.Columns.add("Keys")
dt.loaddatarow(new object() {"Martin", "1"},true)
dt.loaddatarow(new object() {"Cor","2"},true)
'This can all in probably hundred other ways, this is the way I do it.

Combobox1.datasource = dt
Combobox1.displaymember = "Names"
Combobox1.Valuemember = "Keys"
///

I hope this helps,

Cor
 
H

Herfried K. Wagner [MVP]

Martin said:
How do you store a key for each item in a combobox?

\\\
Me.ComboBox1.Items.Add(New Person("Pink Panther", 22)

' Test.
MsgBox(DirectCast(Me.ComboBox1­.Items(0), Person).ToString())
..
..
..
Public Class Person
Private m_Name As String
Private m_Age As Integer

Public Sub New(ByVal Name As String, ByVal Age As Integer)
Me.Name = Name
Me.Age = Age
End Sub

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property

Public Property Age() As Integer
Get
Return m_Age
End Get
Set(ByVal Value As Integer)
m_Age = Value
End Set
End Property

Public Overrides Function ToString() As String
Return Me.Name & " (" & Me.Age.ToString() & ")"
End Function
End Class
///

Alternatively you could use the control's 'DataSource', 'DisplayMember', and
'ValueMember' properties.
 
G

Guest

This works in VB.Net 2003 as I store objects all the time with the ToString
overridden.
 
C

CMM

While all the suggestions others have posted are perfectly good, sometimes I
find that all I really want is to store key/value pairs into a ComboxBox or
ListBox. For that purpose I create a very basic Key/Value class for this
purpose that I can re-use. .NET 2005 also has some interesting key/value
collections and dictionaries that serve similar purposes.
 
S

ShaneO

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.
 
C

CMM

4. LEGITIMACY: These functions are not derived from some "back-door"
method or even API implementation, they are provided as Functions by
Microsoft.

In the VS2003 MSDN documentation, MS states this:
Caution ... Although it is possible to use this library when writing new
code, there is no guarantee that it will be supported in future versions of
Visual Basic.

Having said that, I don't see this warning in the VS2005 documentation.
Given MS's religious obsession with backwards-compatibility, I can see them
deciding to tow this namespace for some time. Still, it's something to be
weary of.

Just my 2c.
 
S

ShaneO

CMM said:
In the VS2003 MSDN documentation, MS states this:
Caution ... Although it is possible to use this library when writing new
code, there is no guarantee that it will be supported in future versions of
Visual Basic.

Having said that, I don't see this warning in the VS2005 documentation.
Given MS's religious obsession with backwards-compatibility, I can see them
deciding to tow this namespace for some time. Still, it's something to be
weary of.

Just my 2c.
Point taken. Before I opted to use these Functions myself, I did come
across the following -

"Although the functions and objects in the Compatibility namespace were
designed to support the upgrade tool, there is nothing to prevent you
from using them when creating a new application in Visual Basic 2005. In
most cases, however, the .NET Framework provides richer functionality."

(Taken from Par 4 of the Visual Basic 6.0 Compatibility Library -
http://msdn2.microsoft.com/en-us/library/wk6ka2wf.aspx)

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
C

Cor Ligthert [MVP]

Martin,

Nobody including me did tell the advance of this concept.

Now you can build one time a table or array (Object) and reuse that endless
times.

Cor
 
M

Martin

Thank you all for your help. Been waiting for this new functionality for a
long time. Glad to be rid of ItemData ;-)
 
C

CMM

I think the fairly scolding "we may not support this in some future version"
disclaimer present in the VB2003 (and VB2002?) documentation put off a lot
of people from using that namespace. I wouldn't mind seeing some of its
features moved or duplicated into the main VB namespaces.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top