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. Selecting a value from a combo box using code

Selecting a value from a combo box using code

Scheduled Pinned Locked Moved Visual Basic
helptutorial
3 Posts 2 Posters 0 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.
  • M Offline
    M Offline
    mincefish
    wrote on last edited by
    #1

    I've got an issue with trying to select a value from a combo box, using code. I have a form that opens up, with a bunch of controls. This form represents a record in a table. I'm currently using a dataset to go and get that data, and then populating the controls with the individual values from that dataset. There is a combo box on the form that is populated using an object that holds the ID of the value, and the display text. These are then assigned to the combo box as ValueMember and DisplayMember respectively. When I ask for the SelectedValue, I get the ValueMember, which is all well and good. However, what I want to do, is when the form loads, I want to return a dataset containing the ID that relates the ValueMember of the combo box, and select the value in the combo box that relates to that value, so that I get the corresponding DisplayMember. I've got no idea how to do this at the moment, and it's driving me potty! Anyone...anyone... Bueller! TIA Tom

    D 1 Reply Last reply
    0
    • M mincefish

      I've got an issue with trying to select a value from a combo box, using code. I have a form that opens up, with a bunch of controls. This form represents a record in a table. I'm currently using a dataset to go and get that data, and then populating the controls with the individual values from that dataset. There is a combo box on the form that is populated using an object that holds the ID of the value, and the display text. These are then assigned to the combo box as ValueMember and DisplayMember respectively. When I ask for the SelectedValue, I get the ValueMember, which is all well and good. However, what I want to do, is when the form loads, I want to return a dataset containing the ID that relates the ValueMember of the combo box, and select the value in the combo box that relates to that value, so that I get the corresponding DisplayMember. I've got no idea how to do this at the moment, and it's driving me potty! Anyone...anyone... Bueller! TIA Tom

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      If I understand the problem correctly... You're trying to select a value in a combobox using code? Correct? Notice that SelectedValue is a get/SET property. You can set the value of the SelectedValue property to the ValueMember that you want and it should show you the corresponding DisplayMember in the ComboBox. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        If I understand the problem correctly... You're trying to select a value in a combobox using code? Correct? Notice that SelectedValue is a get/SET property. You can set the value of the SelectedValue property to the ValueMember that you want and it should show you the corresponding DisplayMember in the ComboBox. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        M Offline
        M Offline
        mincefish
        wrote on last edited by
        #3

        Thanks - just by writing a response, trying to show there was nothing wrong with my code, I've realised what I was doing wrong. The class I use to create an object to fill the combo box holds the ID as Long, and I was casting the value coming from the dataset as an Integer. Once I changed the cast to a Long, it worked like a dream. I'm sorry for wasting your time! Many Thanks Tom

        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