Home All Groups Group Topic Archive Search About
Author
5 Feb 2006 11:06 AM
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

Author
5 Feb 2006 11:14 AM
Armin Zingler
Show quote Hide quote
"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
Author
5 Feb 2006 3:08 PM
Dennis
This works in VB.Net 2003 as I store objects all the time with the ToString
overridden.
--
Dennis in Houston


Show quoteHide quote
"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
>
>
Author
5 Feb 2006 11:33 AM
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
Author
5 Feb 2006 11:58 AM
Herfried K. Wagner [MVP]
"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/>
Author
5 Feb 2006 6:48 PM
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.
Author
6 Feb 2006 1:30 AM
ShaneO
Martin wrote:
Show quoteHide quote
> 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.
Author
6 Feb 2006 2:00 AM
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.
Author
6 Feb 2006 2:42 AM
ShaneO
CMM wrote:
Show quoteHide quote
>>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.
Author
7 Feb 2006 6:25 PM
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.
Author
6 Feb 2006 6:45 AM
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
Author
6 Feb 2006 11:27 AM
Martin
Thank you all for your help. Been waiting for this new functionality for a
long time. Glad to be rid of ItemData ;-)


Show quoteHide quote
"Martin" <x@y.com> wrote in message
news:eM6tARkKGHA.2184@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
>