473,382 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Drop Down

Is there an easy way to store a value with an ID in a drop
down list. ie making it an array and showing only the
value and not an ID. I believe this was possible in VB 6.

So that when they select a value I can get its
corresponding ID that is stored in a database for example.

If not what would be the easiest way to do this?

Thank you.

Sebastian
Nov 20 '05 #1
2 2849
Hi Sebastian,

First off, I'm assuming that you're talking about WinForms, not WebForms.

The ListBox has an Item collection which accepts any sort of object. (They
needn't even be the same sort within a given list).

To display the list the ListBox calls the ToString() function of each
item. In the case of strings and numbers this will give the string or number.
In the case of objects it will give whatever has been coded. If there is no
ToString() then the default is that of Object which returns the object's type
name.

So if you define an object (class or structure, class perhaps better) and
override the ToString() then you can have your list display what you want and
have access to any associated information.

'This is not the best way to design a class, but it shows the principle.
Public Class NameAndIdForListBox
Public Name As String
Public Id As Integer
Public Sub New (ThisName As String, ThisId As Integer)
Name = ThisName
Id = ThisId
End Sub
Public Overrides Function ToString As String
Return Name
End Function
End Class

ListBox1.Items.Add (New NameAndIdForListBox ("Foo", 23))
ListBox1.Items.Add (New NameAndIdForListBox ("Bar", 77))
ListBox1.Items.Add ("And now for something completely different.")

Regards,
Fergus
Nov 20 '05 #2
Hello,

"Sebastian Santacroce" <ss*********@ilogic.com> schrieb:
Is there an easy way to store a value with an ID in a drop
down list. ie making it an array and showing only the
value and not an ID. I believe this was possible in VB 6.

So that when they select a value I can get its
corresponding ID that is stored in a database for example.

If not what would be the easiest way to do this?


\\\
Dim p As New Person()
p.Name = "Pink Panther"
p.Age = 22

Me.ComboBox1.Items.Add(p)

MessageBox.Show( _
DirectCast(Me.ComboBox1.Items.Item(0), Person).ToString() _
)
..
..
..
Public Class Person
Private m_strName As Object
Private m_intAge As Integer

Public Property Name() As String
Get
Return m_strName
End Get
Set(ByVal Value As String)
m_strName = Value
End Set
End Property

Public Property Age() As Integer
Get
Return m_intAge
End Get
Set(ByVal Value As Integer)
m_intAge = Value
End Set
End Property

Public Overrides Function ToString() As String
Return m_strName & " (" & m_intAge.ToString() & ")"
End Function
End Class
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #3

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

Similar topics

2
by: ehm | last post by:
I am working on creating an editable grid (for use in adding, deleting, and editing rows back to an Oracle database). I have a JSP that posts back to a servlet, which in turns posts to a WebLogic...
3
by: Don Wash | last post by:
Hi There! I have a Server-side Drop-down box in ASP.NET (VB) page. What do I do to widen the Drop down box's Pull-Down list's width? I'm not talking about the Drop-down box's width but the box...
2
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will...
4
by: TycoonUK | last post by:
Hi, As I do not have IE7 on my computer, I was wondering if there is a fault in my CSS Menu when using IE7. Please can someone look at my site - http://www.worldofmonopoly.co.uk and tell me...
3
by: penny111 | last post by:
Hi there, For my application, i need to have 3 drop down lists 1. drop down list of folder names 2. drop down list of documents in the folder selected 3. drop down list of instances of the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.