Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. ComboBox help

ComboBox help

Scheduled Pinned Locked Moved Visual Basic
help
10 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RyJaBy
    wrote on last edited by
    #1

    Hi, I need help displaying a list of items in a combo box. My problem is this, I want to display a name "Drive Time" and when it is selected i want to use the value of the variable "002302" it is like: Drive Time = 002302 I want to display "Drive Time" and I want to use the value "002302" when it is selected in the combobox. I hope this made sense. I appreciate the help. Thanks

    W S P N 4 Replies Last reply
    0
    • R RyJaBy

      Hi, I need help displaying a list of items in a combo box. My problem is this, I want to display a name "Drive Time" and when it is selected i want to use the value of the variable "002302" it is like: Drive Time = 002302 I want to display "Drive Time" and I want to use the value "002302" when it is selected in the combobox. I hope this made sense. I appreciate the help. Thanks

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      It would be best if you created a structure to hold both items as a single datatype, and then you create a List(Of item) and bind the ComboBox to the List(Of T). Then in your ComboBox.SelectedIndexChanged you can reset the textbox Like this

      Public Class Form1

      Private Structure Items
      
          Private strName As String
          Private intID As Integer
          Public Property Name() As String
              Get
                  Return strName
              End Get
              Set(ByVal value As String)
                  strName = value
              End Set
          End Property
          Public Property ID() As Integer
              Get
                  Return intID
              End Get
              Set(ByVal value As Integer)
                  intID = value
              End Set
          End Property
      End Structure
      
      Private lstItems As List(Of Items)
      
      Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
          lstItems = New List(Of Items)
          For i As Integer = 0 To 5
              Dim newItem As New Items
              newItem.Name = "Item " & i.ToString
              newItem.ID = i
              lstItems.Add(newItem)
          Next
          ComboBox1.DataSource = lstItems
          ComboBox1.DisplayMember = "Name"
          ComboBox1.ValueMember = "ID"
      
      End Sub
      
      Private Sub ComboBox1\_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
      
          TextBox1.Text = CType(ComboBox1.SelectedItem, Items).Name
      
      End Sub
      

      End Class

      Hope this helps Happy Coding

      R 1 Reply Last reply
      0
      • R RyJaBy

        Hi, I need help displaying a list of items in a combo box. My problem is this, I want to display a name "Drive Time" and when it is selected i want to use the value of the variable "002302" it is like: Drive Time = 002302 I want to display "Drive Time" and I want to use the value "002302" when it is selected in the combobox. I hope this made sense. I appreciate the help. Thanks

        S Offline
        S Offline
        Scubapro
        wrote on last edited by
        #3

        Yes, makes sense. Why don't you use an ArrayList in conjuction with your ComboBox and make the indexes correspond to one another? E.g.

        ComboBox1.Items.Add("Drive Time")
        ComboBox1.Items.Add("......")
        ...
        Array1.Add("002302")
        Array1.Add("......")
        ...

        Public Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        value = Array1.Item(ComboBox1.SelectedIndex).ToString

        End Sub

        R L 2 Replies Last reply
        0
        • S Scubapro

          Yes, makes sense. Why don't you use an ArrayList in conjuction with your ComboBox and make the indexes correspond to one another? E.g.

          ComboBox1.Items.Add("Drive Time")
          ComboBox1.Items.Add("......")
          ...
          Array1.Add("002302")
          Array1.Add("......")
          ...

          Public Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

          value = Array1.Item(ComboBox1.SelectedIndex).ToString

          End Sub

          R Offline
          R Offline
          RyJaBy
          wrote on last edited by
          #4

          Thank you! that makes so much sense.

          1 Reply Last reply
          0
          • W Wayne Gaylard

            It would be best if you created a structure to hold both items as a single datatype, and then you create a List(Of item) and bind the ComboBox to the List(Of T). Then in your ComboBox.SelectedIndexChanged you can reset the textbox Like this

            Public Class Form1

            Private Structure Items
            
                Private strName As String
                Private intID As Integer
                Public Property Name() As String
                    Get
                        Return strName
                    End Get
                    Set(ByVal value As String)
                        strName = value
                    End Set
                End Property
                Public Property ID() As Integer
                    Get
                        Return intID
                    End Get
                    Set(ByVal value As Integer)
                        intID = value
                    End Set
                End Property
            End Structure
            
            Private lstItems As List(Of Items)
            
            Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
                lstItems = New List(Of Items)
                For i As Integer = 0 To 5
                    Dim newItem As New Items
                    newItem.Name = "Item " & i.ToString
                    newItem.ID = i
                    lstItems.Add(newItem)
                Next
                ComboBox1.DataSource = lstItems
                ComboBox1.DisplayMember = "Name"
                ComboBox1.ValueMember = "ID"
            
            End Sub
            
            Private Sub ComboBox1\_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            
                TextBox1.Text = CType(ComboBox1.SelectedItem, Items).Name
            
            End Sub
            

            End Class

            Hope this helps Happy Coding

            R Offline
            R Offline
            RyJaBy
            wrote on last edited by
            #5

            Thanks for the help!

            1 Reply Last reply
            0
            • S Scubapro

              Yes, makes sense. Why don't you use an ArrayList in conjuction with your ComboBox and make the indexes correspond to one another? E.g.

              ComboBox1.Items.Add("Drive Time")
              ComboBox1.Items.Add("......")
              ...
              Array1.Add("002302")
              Array1.Add("......")
              ...

              Public Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

              value = Array1.Item(ComboBox1.SelectedIndex).ToString

              End Sub

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              That is horrible. It was OK in the eighties perhaps, now things get to be handled in an object-oriented way, so turn those things into objects, show one of their properties and use another property. BTW: that is exactly what ComboBox.DisplayMember and ValueMember properties are for. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              S 1 Reply Last reply
              0
              • L Luc Pattyn

                That is horrible. It was OK in the eighties perhaps, now things get to be handled in an object-oriented way, so turn those things into objects, show one of their properties and use another property. BTW: that is exactly what ComboBox.DisplayMember and ValueMember properties are for. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                S Offline
                S Offline
                Scubapro
                wrote on last edited by
                #7

                Simia est simia et si aurea gestet insignia.

                1 Reply Last reply
                0
                • R RyJaBy

                  Hi, I need help displaying a list of items in a combo box. My problem is this, I want to display a name "Drive Time" and when it is selected i want to use the value of the variable "002302" it is like: Drive Time = 002302 I want to display "Drive Time" and I want to use the value "002302" when it is selected in the combobox. I hope this made sense. I appreciate the help. Thanks

                  P Offline
                  P Offline
                  programmervb netc
                  wrote on last edited by
                  #8

                  I hate to admit it but I just recently learned this. I believe what you are trying to do is display one value and use another that is associated with it for something else. The easiest way I have seen this done is to use the DisplayMember and the ValueMember properties of the combo box like I said I just figured this out and it has made my life so much easier than the old way we handled it. So in your case you would use.

                  cboMyCombo.DisplayMember = "Drive Time"
                  cboMyCombo.ValueMember = "002302"

                  Msgbox(cboMyCombo.ValueMember.ToString())

                  I did not compile that code it may or may not work but you get the general idea.

                  Humble Programmer

                  S 1 Reply Last reply
                  0
                  • P programmervb netc

                    I hate to admit it but I just recently learned this. I believe what you are trying to do is display one value and use another that is associated with it for something else. The easiest way I have seen this done is to use the DisplayMember and the ValueMember properties of the combo box like I said I just figured this out and it has made my life so much easier than the old way we handled it. So in your case you would use.

                    cboMyCombo.DisplayMember = "Drive Time"
                    cboMyCombo.ValueMember = "002302"

                    Msgbox(cboMyCombo.ValueMember.ToString())

                    I did not compile that code it may or may not work but you get the general idea.

                    Humble Programmer

                    S Offline
                    S Offline
                    Scubapro
                    wrote on last edited by
                    #9

                    Alas, this won't work, since .DisplayMember and .ValueMember are string based which refer to a corresponding (table)column name of the bonded data.

                    1 Reply Last reply
                    0
                    • R RyJaBy

                      Hi, I need help displaying a list of items in a combo box. My problem is this, I want to display a name "Drive Time" and when it is selected i want to use the value of the variable "002302" it is like: Drive Time = 002302 I want to display "Drive Time" and I want to use the value "002302" when it is selected in the combobox. I hope this made sense. I appreciate the help. Thanks

                      N Offline
                      N Offline
                      Neville Nazerane
                      wrote on last edited by
                      #10

                      I think the best solution for this is using an enum

                      private enum combo_values
                      Drive_Time = 002302
                      .....
                      .....
                      end enum

                      this is the code for diclareing an enum. Unfortunataly ths would mean that you would have to use an underscore instead of a space. But I guess the user would not mind. So, once the enum is diclared, you can enter the following code when an item is selected:

                      myValue=ctype(combo1.text,enum)

                      if you want make 'combo_values' a variable of your type, you can do it by typing something like,' private enum combo_values as integer'. and do inform me if it does not work.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups