By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

Combobox in VB2005

P: n/a
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
Feb 5 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
"Martin" <x@y.com> schrieb
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

Feb 5 '06 #2

P: n/a
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
Feb 5 '06 #3

P: n/a
"Martin" <x@y.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Feb 5 '06 #4

P: n/a
This works in VB.Net 2003 as I store objects all the time with the ToString
overridden.
--
Dennis in Houston
"Armin Zingler" wrote:
"Martin" <x@y.com> schrieb
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

Feb 5 '06 #5

P: n/a
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.
Feb 5 '06 #6

P: n/a
Martin wrote:
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.SetItemDat a
+
Microsoft.VisualBasic.Compatibility.VB6.GetItemDat a

To use (ListBox or ComboBox) -

iNewIndex = ListBox1.Items.Add(sWhatever) 'String to Display
Microsoft.VisualBasic.Compatibility.VB6.SetItemDat a(ListBox1, iNewIndex,
iKeyCounter) 'iKeyCounter is a unique Index/Key integer
When the User clicks on your ListBox/ComboBox -

iKey = Microsoft.VisualBasic.Compatibility.VB6.GetItemDat a(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.
Feb 6 '06 #7

P: n/a
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.
Feb 6 '06 #8

P: n/a
CMM wrote:
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.

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.
Feb 6 '06 #9

P: n/a
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
Feb 6 '06 #10

P: n/a
Thank you all for your help. Been waiting for this new functionality for a
long time. Glad to be rid of ItemData ;-)
"Martin" <x@y.com> wrote in message
news:eM**************@TK2MSFTNGP10.phx.gbl...
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

Feb 6 '06 #11

P: n/a
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.
Feb 7 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.