473,771 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple values in one line of Combo or Listbox

I have the (usual) problem that, in a combo or listbox, I want to keep a text
and a related key value. For instance the country description and the country
code.
In order to solve this I uss the following approach:

With the following class I create an item

#Region "Create SubClass Item"
Public Class KeyItem
Inherits ListViewItem
Public ItemKey As String
Public ItemText As String
Sub New(ByVal ShowItemKey As String, ByVal ShowItemText As String)
MyBase.New()
ItemKey = ShowItemKey
ItemText = ShowItemText
Me.Text = ItemText
End Sub
End Class
#End Region

then I add records to the combo ...

cmb_country.Ite ms.Add(New KeyItem("NL", "Netherland s"))

Although the values are correctly stored in the combo, when displaying the
values instead of seeing "Netherland s" I see : "ListViewIt em: {Netherlands}"

How can I get rid of that "ListViewItem:{ " part?
Is there an alternate solution?

Thanks for your help

tino
Nov 21 '05 #1
4 2949
"tino" <ti**@discussio ns.microsoft.co m> schrieb:
I have the (usual) problem that, in a combo or listbox, I want to keep a
text
and a related key value.


\\\
Dim p As New Person()
p.Name = "Pink Panther"
p.Age = 22
Me.ComboBox1.It ems.Add(p)

' 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 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 can use a bound combobox:

\\\
With Me.ListBox1

' This can be an 'ArrayList' or array too, for example!
.DataSource = Database.FindPe ople(...)
.DisplayMember = "Name"
.ValueMember = "Age"
End With
///

BTW: ListViewItem has nothing to do with comboboxes or listboxes, it's
related to listview controls.

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

Nov 21 '05 #2
Hi Tino

try this:

'In your form

Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
_ Handles MyBase.Load
cmb_country.Ite ms.Add(New KeyItem("NL", "Netherland s"))

End Sub

Private Sub cmb_country_Sel ectedIndexChang ed(ByVal sender As Object, ByVal e
As_ System.EventArg s) Handles cmb_country.Sel ectedIndexChang ed
MsgBox(DirectCa st(cmb_country. SelectedItem, KeyItem).ItemKe y)
End Sub
'Your item
#Region "Create SubClass Item"
Public Class KeyItem
Public ItemKey As String
Public ItemText As String
Sub New(ByVal ItemKey As String, ByVal ItemText As String)
MyBase.New()
Me.ItemKey = ItemKey
Me.ItemText = ItemText
End Sub
Public Overrides Function tostring() As String
Return ItemText
End Function
End Class
#End Region

hth Peter

"tino" <ti**@discussio ns.microsoft.co m> wrote in message
news:0E******** *************** ***********@mic rosoft.com...
I have the (usual) problem that, in a combo or listbox, I want to keep a text and a related key value. For instance the country description and the country code.
In order to solve this I uss the following approach:

With the following class I create an item

#Region "Create SubClass Item"
Public Class KeyItem
Inherits ListViewItem
Public ItemKey As String
Public ItemText As String
Sub New(ByVal ShowItemKey As String, ByVal ShowItemText As String)
MyBase.New()
ItemKey = ShowItemKey
ItemText = ShowItemText
Me.Text = ItemText
End Sub
End Class
#End Region

then I add records to the combo ...

cmb_country.Ite ms.Add(New KeyItem("NL", "Netherland s"))

Although the values are correctly stored in the combo, when displaying the
values instead of seeing "Netherland s" I see : "ListViewIt em: {Netherlands}"
How can I get rid of that "ListViewItem:{ " part?
Is there an alternate solution?

Thanks for your help

tino

Nov 21 '05 #3
Thanks very much

Peter, your solution is excellent.

Herfried, unofrtunately with your solution the combobox did not show the
value but the object name instead.

Maybe it would be interesting if future VB versions would allow for a hidden
key value in list or combo boxes.

regards
"Peter Proost" wrote:
Hi Tino

try this:

'In your form

Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
_ Handles MyBase.Load
cmb_country.Ite ms.Add(New KeyItem("NL", "Netherland s"))

End Sub

Private Sub cmb_country_Sel ectedIndexChang ed(ByVal sender As Object, ByVal e
As_ System.EventArg s) Handles cmb_country.Sel ectedIndexChang ed
MsgBox(DirectCa st(cmb_country. SelectedItem, KeyItem).ItemKe y)
End Sub
'Your item
#Region "Create SubClass Item"
Public Class KeyItem
Public ItemKey As String
Public ItemText As String
Sub New(ByVal ItemKey As String, ByVal ItemText As String)
MyBase.New()
Me.ItemKey = ItemKey
Me.ItemText = ItemText
End Sub
Public Overrides Function tostring() As String
Return ItemText
End Function
End Class
#End Region

hth Peter

"tino" <ti**@discussio ns.microsoft.co m> wrote in message
news:0E******** *************** ***********@mic rosoft.com...
I have the (usual) problem that, in a combo or listbox, I want to keep a

text
and a related key value. For instance the country description and the

country
code.
In order to solve this I uss the following approach:

With the following class I create an item

#Region "Create SubClass Item"
Public Class KeyItem
Inherits ListViewItem
Public ItemKey As String
Public ItemText As String
Sub New(ByVal ShowItemKey As String, ByVal ShowItemText As String)
MyBase.New()
ItemKey = ShowItemKey
ItemText = ShowItemText
Me.Text = ItemText
End Sub
End Class
#End Region

then I add records to the combo ...

cmb_country.Ite ms.Add(New KeyItem("NL", "Netherland s"))

Although the values are correctly stored in the combo, when displaying the
values instead of seeing "Netherland s" I see : "ListViewIt em:

{Netherlands}"

How can I get rid of that "ListViewItem:{ " part?
Is there an alternate solution?

Thanks for your help

tino


Nov 21 '05 #4
"tino" <ti**@discussio ns.microsoft.co m> schrieb:
Herfried, unofrtunately with your solution the combobox did not show the
value but the object name instead.


You can easily change the item's 'ToString' implementation for the first
part of my solution. For the second part, you will have to change the
'DisplayMember' .

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

Nov 21 '05 #5

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

Similar topics

1
2650
by: Mike | last post by:
My users have to select an value from a fixed selection of values. The obvious choice of control for such a requirement is to use a <select> (i.e. a combo box). My problem is that sometimes, these combo boxes will have a *large* number of values. There could be any number of values in them from 5 to 5 million (unlikely it would be this large but possible). Obviously 5 million is far too much to populate a <select> control with. Does...
2
1717
by: steve | last post by:
Hello, I am using ACCESS 2000 and am currently trying to make a database for a police department and they have several types of license classes. i.e A,B,C,D,E etc. What they have asked for is a drop down box with all the types listed and simple the ability to click on multiple check boxes to show what class(es) of license the person has. I have read through most of the articles on multiple list boxes but this is not my need. I dont need...
2
6592
by: Jen F. | last post by:
I have inherited a medical database in which there are multiple values stored in a single field (ie. "Current Conditions" field might contain 1-20 different conditions, separated by comma (ie. "Heart Disease,Hyper Tyroid,Cancer" etc. I would like to search via combo box for any one or more than one value in this field, ie-what patients have Heart Disease or Cancer by selecting these disorders off the combo box list. Any help would be...
1
1622
by: dburns | last post by:
I have an unbound ListBox and an unbound ComboBox on the same form. I have them both set up to display the values from the same field in the same table. The ListBox displays the correct values; the ComboBox displays nothing. The form is just a test form I'm using to sort this problem out. It has no other controls on it and no code associated with any portion. The table is a very simple table, set up just to sort this out. I have tried...
3
2391
by: Jay Ruyle | last post by:
I'm trying to figure out a way to list several items in a listbox and let the user select any number of items in the listbox. I have tried to code in the items as bitwise items but all it stores in the database is the top item in the listbox. How can I get the listbox to add all the bit values to be stored and also come out properly when viewed later? Example:
7
4094
by: technocraze | last post by:
Hi guys, I encountered this error while using the AfterUpdate event for my listbox. Error: Update or CancelUpdate without using AddNew or Edit. What i wanted to achieve is just to display the selected values from the listbox into the textboxes. I have bounded both my listbox and textbox to the controlSource of the field of my table. I have two combo boxes. One is getting the from the query and other is matching the corresponding values...
1
2500
by: Intrepid_Yellow | last post by:
Hi, I have the following code that runs my report generator. The user selects a table from a combo box, then whatever fields they want from a list box. (This part all works and the report runs fine). There is then a combo box they can select a field from (eg CompanyID etc) and then the list box below that contains the values (eg Microsoft, Novell etc). These are all multi-select list boxes. Now I can get the code to work if the user...
6
9429
by: Dave | last post by:
On my form I have combo boxes. These combo boxes, after updating them, populate respective listboxes that are located below the combo boxes on the same form. I am trying to use a "generate report" button located on my form to print all of the list box values (that have been updated via selection from combo boxes) from the form to the report. I've tried using a macro with the code: Macro Name: cmdGenerateReport : On Click Action:...
12
4047
by: micarl | last post by:
How would i print a report based on criteria selected from several Combo Boxes as well as multiple Multi Select List Boxes, that are located on the same form? I can get one Multi List Box, just not several, to report using this code i found - Private Sub cmdPreview_Click()
0
10260
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9910
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4007
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 we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.