Combobox and Values - Help Please

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TheGanjaMan

    #1

    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...
  • Al Reid

    #2
    Re: Combobox and Values - Help Please

    "TheGanjaMa n" <theganjaman@so mplace.invalid> wrote in message news:Xns976A8BE 526517TheGanjaM an@207.46.248.1 6...[color=blue]
    > 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...[/color]

    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("Cana da", "1")

    I hope this helps.

    --
    Al Reid


    Comment

    • TheGanjaMan

      #3
      Re: Combobox and Values - Help Please

      "Al Reid" <areidjr@reidDA SHhome.com> wrote in
      news:#opNZFWMGH A.4052@TK2MSFTN GP15.phx.gbl:
      [color=blue]
      > "TheGanjaMa n" <theganjaman@so mplace.invalid> wrote in message
      > news:Xns976A8BE 526517TheGanjaM an@207.46.248.1 6...[color=green]
      >> Hi all,
      >>
      >> I'm pretty new to programming with vb.net. I'm stuck on a problem:[/color][/color]
      ....[color=blue][color=green]
      >>
      >> Any help would be appreciated.
      >>
      >> Thanks...[/color]
      >
      > 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.[/color]
      ....[color=blue]
      > End Class
      > =============== =======
      >
      > Then you can add the Items and ItemData to the ComboBox using
      >
      > Combobox1.Items .Add(New ComboItem("Cana da", "1")
      >
      > I hope this helps.
      >
      > --
      > Al Reid
      >
      >[/color]
      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.selec tedvalue 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("f orm_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_Selec tedIndexChanged (ByVal sender As
      System.Object, ByVal e As System.EventArg s) Handles
      ComboBox1.Selec tedIndexChanged
      TextBox2.Text = ComboBox1.Selec tedValue & " - " &
      ComboBox1.Selec tedItem
      End Sub
      I get en error and when I do this:
      Private Sub ComboBox1_Selec tedIndexChanged (ByVal sender As
      System.Object, ByVal e As System.EventArg s) Handles
      ComboBox1.Selec tedIndexChanged
      TextBox2.Text = ComboBox1.Selec tedValue
      End Sub

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

      Comment

      • Al Reid

        #4
        Re: Combobox and Values - Help Please

        "TheGanjaMa n" <theganjaman@so mplace.invalid> wrote in message news:Xns976A95E 3821BETheGanjaM an@207.46.248.1 6...[color=blue]
        > 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.selec tedvalue 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("f orm_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_Selec tedIndexChanged (ByVal sender As
        > System.Object, ByVal e As System.EventArg s) Handles
        > ComboBox1.Selec tedIndexChanged
        > TextBox2.Text = ComboBox1.Selec tedValue & " - " &
        > ComboBox1.Selec tedItem
        > End Sub
        > I get en error and when I do this:
        > Private Sub ComboBox1_Selec tedIndexChanged (ByVal sender As
        > System.Object, ByVal e As System.EventArg s) Handles
        > ComboBox1.Selec tedIndexChanged
        > TextBox2.Text = ComboBox1.Selec tedValue
        > End Sub
        >
        > I get nothing... any ideas?
        > (Thanks very much for the example code...)
        >[/color]

        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).Item Data <- to get the country code

        --
        Al Reid


        Comment

        • TheGanjaMan

          #5
          Re: Combobox and Values - Help Please

          "Al Reid" <areidjr@reidDA SHhome.com> wrote in
          news:uSSqOXWMGH A.2472@TK2MSFTN GP11.phx.gbl:

          [snip...][color=blue]
          >
          > 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).Item Data <- to get the country code
          >
          > --
          > Al Reid
          >
          >
          >[/color]

          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

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Combobox and Values - Help Please

            "TheGanjaMa n" <theganjaman@so mplace.invalid> schrieb:[color=blue]
            > 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.[/color]

            <URL:http://groups.google.d e/group/microsoft.publi c.dotnet.langua ges.vb/msg/029eaf619366a27 4>

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

            Comment

            Working...