By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,556 Members | 1,567 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,556 IT Pros & Developers. It's quick & easy.

Combobox and Values - Help Please

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
"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

P: n/a
"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

P: n/a
"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

P: n/a
"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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.