473,385 Members | 1,356 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,385 software developers and data experts.

Combobox and Values - Help Please

Hi all,

I'm pretty new to programming with vb.net. I'm stuck on a problem:
I need to populate a combobox with items and a value for each item.

For example if it were for countries:
Item - Value
Canada - 1
France - 2
Egypt - 3
Brazil - 1
Germany - 2
Spain - 2
and such...

I want the user to see the Item (Country name) but when he selects the
country, I want another event to happen based on the Value.
I was able to do such things with old ASP and VBScript code, but with
..net it doesn't seem the same.
I tried: Combobox1.Items.Add("Canada", "U"), but it didn't work.

Any help would be appreciated.

Thanks...
Feb 14 '06 #1
5 1561
"TheGanjaMan" <th*********@somplace.invalid> wrote in message news:Xn*************************@207.46.248.16...
Hi all,

I'm pretty new to programming with vb.net. I'm stuck on a problem:
I need to populate a combobox with items and a value for each item.

For example if it were for countries:
Item - Value
Canada - 1
France - 2
Egypt - 3
Brazil - 1
Germany - 2
Spain - 2
and such...

I want the user to see the Item (Country name) but when he selects the
country, I want another event to happen based on the Value.
I was able to do such things with old ASP and VBScript code, but with
.net it doesn't seem the same.
I tried: Combobox1.Items.Add("Canada", "U"), but it didn't work.

Any help would be appreciated.

Thanks...


The Add method expects a single object to be passed to it. If you want to add a combo item and item data like you could in vb6
create a ComboIItem class and pass an instance to the Add method.

=======================
Public Class ComboItem
' Declare the variable the property uses.
Private strItemData As String
Private strItem As String
Public Property ItemData() As String
Get
Return strItemData
End Get
Set(ByVal ItemData As String)
strItemData = ItemData
End Set
End Property

Public Property Item() As String
Get
Return strItem
End Get
Set(ByVal Item As String)
strItem = Item
End Set
End Property
Public Overrides Function ToString() As String
Return strItem
End Function
Public Sub New()
End Sub
Public Sub New(ByVal Item As String, ByVal ItemData As String)
strItem = Item
strItemData = ItemData
End Sub
End Class
======================

Then you can add the Items and ItemData to the ComboBox using

Combobox1.Items.Add(New ComboItem("Canada", "1")

I hope this helps.

--
Al Reid
Feb 14 '06 #2
"Al Reid" <ar*****@reidDASHhome.com> wrote in
news:#o**************@TK2MSFTNGP15.phx.gbl:
"TheGanjaMan" <th*********@somplace.invalid> wrote in message
news:Xn*************************@207.46.248.16...
Hi all,

I'm pretty new to programming with vb.net. I'm stuck on a problem: ....
Any help would be appreciated.

Thanks...
The Add method expects a single object to be passed to it. If you
want to add a combo item and item data like you could in vb6 create a
ComboIItem class and pass an instance to the Add method.

=======================
Public Class ComboItem
' Declare the variable the property uses.

.... End Class
======================

Then you can add the Items and ItemData to the ComboBox using

Combobox1.Items.Add(New ComboItem("Canada", "1")

I hope this helps.

--
Al Reid

Thanks... I'm pretty new to OOP so I think I need time for the code in the
class to sink in.
I did put your code into a new class in my project and now my combobox
accepts the values I assign to it. but when I try to read the
combobox1.selectedvalue I still get nothing. What am I doing wrong?
I have the following code to fill the combobox:
Public Sub FillMenu()
Dim conn = DBConn()
Dim sqlstr As String = "select * from Form_Table"
Dim rs = DBrs(conn, sqlstr)
Do While rs.read
ComboBox1.Items.Add(New ComboItem(rs("form_name"), rs("T_id")))
Loop
conn.close()
End Sub
The above code works as it fills the combobox... but when I do this:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
TextBox2.Text = ComboBox1.SelectedValue & " - " &
ComboBox1.SelectedItem
End Sub
I get en error and when I do this:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
TextBox2.Text = ComboBox1.SelectedValue
End Sub

I get nothing... any ideas?
(Thanks very much for the example code...)

Feb 14 '06 #3
"TheGanjaMan" <th*********@somplace.invalid> wrote in message news:Xn*************************@207.46.248.16...
Thanks... I'm pretty new to OOP so I think I need time for the code in the
class to sink in.
I did put your code into a new class in my project and now my combobox
accepts the values I assign to it. but when I try to read the
combobox1.selectedvalue I still get nothing. What am I doing wrong?
I have the following code to fill the combobox:
Public Sub FillMenu()
Dim conn = DBConn()
Dim sqlstr As String = "select * from Form_Table"
Dim rs = DBrs(conn, sqlstr)
Do While rs.read
ComboBox1.Items.Add(New ComboItem(rs("form_name"), rs("T_id")))
Loop
conn.close()
End Sub
The above code works as it fills the combobox... but when I do this:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
TextBox2.Text = ComboBox1.SelectedValue & " - " &
ComboBox1.SelectedItem
End Sub
I get en error and when I do this:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
TextBox2.Text = ComboBox1.SelectedValue
End Sub

I get nothing... any ideas?
(Thanks very much for the example code...)


SelectedItem returns an object as well. In this case it's an instance of
the ComboItem class. Therefore, you need to access the properties of the
object in order to use them.

TextBox2.Text = CType(ComboBox1.SelectedItem, ComboItem).Item <- to get the country name OR
TextBox2.Text = CType(ComboBox1.SelectedItem, ComboItem).ItemData <- to get the country code

--
Al Reid
Feb 14 '06 #4
"Al Reid" <ar*****@reidDASHhome.com> wrote in
news:uS**************@TK2MSFTNGP11.phx.gbl:

[snip...]

SelectedItem returns an object as well. In this case it's an instance
of the ComboItem class. Therefore, you need to access the properties
of the object in order to use them.

TextBox2.Text = CType(ComboBox1.SelectedItem, ComboItem).Item <- to
get the country name OR TextBox2.Text = CType(ComboBox1.SelectedItem,
ComboItem).ItemData <- to get the country code

--
Al Reid


Thank you ... it worked like a charm...
and saved me hours of frustration and pulling off whats left of the hair on
my head.

TGM
Feb 14 '06 #5
"TheGanjaMan" <th*********@somplace.invalid> schrieb:
I'm pretty new to programming with vb.net. I'm stuck on a problem:
I need to populate a combobox with items and a value for each item.


<URL:http://groups.google.de/group/microsoft.public.dotnet.languages.vb/msg/029eaf619366a274>

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

Feb 14 '06 #6

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

Similar topics

3
by: Tamir Khason | last post by:
Why this does not work??? cb1 - combobox DataProvider.DataProvider.DS_BringRacks() - DataSet cb1.DataSource=DataProvider.DataProvider.DS_BringRacks().Tables; cb1.DisplayMember = "id"; ...
5
by: Aaron Ackerman | last post by:
I have a bound combobox the appears on a cell within the column of my bound grid when the user clicks on a cell n(In my vb.net WinForm app). I am trying to allow the adding of an item to that bound...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
4
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
1
by: amber | last post by:
I'm having an issue with a combobox that is making no sense to me at all. I have a form with several comboboxes/textboxes. The values in these boxes are based on a datarowview, which is based on...
4
by: JJGarcia | last post by:
Hi Everyone, I'll try to explain the process I'm following, I'm new to this so I'm triying the easy way first, probably the lasyest too! I created a new Project, drag in to it a SQLConnection,...
8
by: | last post by:
I am sure this has been asked and answered, but here goes anyway... VS.Net 2005, VB.Net How can you display more than one field in the displaymember property of a combobox inside the...
1
by: BASSPU03 | last post by:
I've set my combobox to operate according to this option: "Find a record on my form based on a value I selected from my combobox." When I select a value from the combobox, I can see that the...
14
by: Mark | last post by:
I have a table with a field that uses a combobox to populate values. The Lookup tab within table design mode is the following: Display Control Combo Box Row Source Type ...
3
by: Simon van Beek | last post by:
Dear reader, What can be wrong in my ComboBox, the property "Auto Expand" is set to Yes, but by typing in the ComboBox it doesn't expand. Is this because the source of the ComboBox is a...
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
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...

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.