473,473 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Combobox in VB2005

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
11 6589
"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
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
"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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Lars Netzel | last post by:
I have, in earlier projects, had som serious problems with ComboBoxColumns in a datagrid. And now I'm in a projects where I needs sometthing like that again.. but I really do not want to work with...
4
by: Matt | last post by:
I have been searching all over the web for a way to sort a DataGridView based on the actual text being shown in a ComboBox column as opposed to the underlying value (an ID in this case). Can anyone...
6
by: ProteusGak | last post by:
Hi, new membership and first post. I am working in VB 2005, windows forms and would like to know how to open form2 based on the user's selection from a combobox or listbox from say, form1. Thank...
4
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I have to load 30,000 unique names into a combox. Filling a dataTable takes only a few milliseconds. But populating the combobox and displaying the list takes several seconds - way...
3
by: John | last post by:
Does anyone have some sample code for building a combobox that will automatically list the remaining folders while you type? I'm looking for functionality similar to the "Look in:" combobox in the...
11
by: guy | last post by:
I am able to use the Server Explorer to create a dataset containing tables in my database. I am able to bind a table to my combobox and set a filter by including a where clause. But I am unable to...
15
by: Aalaan | last post by:
I am presently a user of classic vb6 and hang out on those newsgroups. Some of you may be aware that there is a very anti MS and vb2005 feeling there. I have tried to get them to tell me which...
2
by: spowel4 | last post by:
Here's my code thus far: Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim objxmldoc As New Xml.XmlDataDocument() 'Load all customer numbers...
0
by: chavito | last post by:
Hi people, I'm a vb2005 learner and I have created a combo box in vb2005 and i want to insert into a database table the ID of an item. The items i got from a database table. I cant do it. Can...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.