Home All Groups Group Topic Archive Search About

Combobox and Values - Help Please

Author
14 Feb 2006 11:44 AM
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...

Author
14 Feb 2006 12:22 PM
Al Reid
Show quote Hide quote
"TheGanjaMan" <theganjaman@somplace.invalid> wrote in message news:Xns976A8BE526517TheGanjaMan@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
Author
14 Feb 2006 12:43 PM
TheGanjaMan
Show quote Hide quote
"Al Reid" <arei***@reidDASHhome.com> wrote in
news:#opNZFWMGHA.4052@TK2MSFTNGP15.phx.gbl:

> "TheGanjaMan" <theganjaman@somplace.invalid> wrote in message
> news:Xns976A8BE526517TheGanjaMan@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...)
Author
14 Feb 2006 12:54 PM
Al Reid
Show quote Hide quote
"TheGanjaMan" <theganjaman@somplace.invalid> wrote in message news:Xns976A95E3821BETheGanjaMan@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
Author
14 Feb 2006 3:19 PM
TheGanjaMan
"Al Reid" <arei***@reidDASHhome.com> wrote in
news:uSSqOXWMGHA.2472@TK2MSFTNGP11.phx.gbl:

[snip...]
Show quoteHide quote
>
> 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
Author
14 Feb 2006 3:31 PM
Herfried K. Wagner [MVP]
"TheGanjaMan" <theganjaman@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/>