Combobox and Values - Help Please

T

TheGanjaMan

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...
 
A

Al Reid

TheGanjaMan said:
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.
 
T

TheGanjaMan

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.
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...)
 
A

Al Reid

TheGanjaMan said:
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
 
T

TheGanjaMan

[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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top