473,545 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
+ 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 6596
"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.whateverYo uWant
end function
end class
Add item to the combo:

cbo.items.add(n ew comboitem(yourO bject))

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"},tru e)
'This can all in probably hundred other ways, this is the way I do it.

Combobox1.datas ource = dt
Combobox1.displ aymember = "Names"
Combobox1.Value member = "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.It ems.Add(New Person("Pink Panther", 22)

' Test.
MsgBox(DirectCa st(Me.ComboBox1 *.Items(0), Person).ToStrin g())
..
..
..
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.whateverYo uWant
end function
end class
Add item to the combo:

cbo.items.add(n ew comboitem(yourO bject))

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.Visua lBasic.Compatib ility namespace (Project>Add
Reference>.NET Tab then highlight "Microsoft.Visu alBasic.Compati bility"
and select OK)

You will now have several new functions, but the two you want are -

Microsoft.Visua lBasic.Compatib ility.VB6.SetIt emData
+
Microsoft.Visua lBasic.Compatib ility.VB6.GetIt emData

To use (ListBox or ComboBox) -

iNewIndex = ListBox1.Items. Add(sWhatever) 'String to Display
Microsoft.Visua lBasic.Compatib ility.VB6.SetIt emData(ListBox1 , iNewIndex,
iKeyCounter) 'iKeyCounter is a unique Index/Key integer
When the User clicks on your ListBox/ComboBox -

iKey = Microsoft.Visua lBasic.Compatib ility.VB6.GetIt emData(ListBox1 ,
ListBox1.Select edIndex)

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1414
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 Datagrid in Windows.Form cause they are buggy and ComboBoxes are not easy to work with there (especially not when you need Multi Columns in the...
4
8619
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 tell me if this is even possible, and if so, how to do it? If this is completely impossible, how would you suggest going about sorting a ComboBox...
6
4898
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 you, PG
4
5093
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 too long. A user selects a name from the combobox and runs a query. Originally, this combox was on a form in an MS Access ADP which was linked...
3
5723
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 Search Companion panel in Windows Explorer. If you type "C:\" it will open the drop down and fill it in with all of the directories on C:\. Then as...
11
2534
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 concat 2 columns as an addition column in the select statement without getting an error. It seems that I am restricted to use only the columns of the...
15
2002
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 features of vb2005 are actually worse then vb6 in practice, and forget the philosophy of backward compatibility for a moment. I would now like to...
2
4092
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 objxmldoc.Load("s:\sw7\nc\custnum.xml") Dim onode As Xml.XmlNode Dim custNum As Xml.XmlNodeList custNum =
0
870
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 somebody help me with it? Thank you so much God bless you
0
7457
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7391
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7410
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7746
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5962
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5320
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4941
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
693
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.