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. .NET (Core and Framework)
  4. combobox find item by value

combobox find item by value

Scheduled Pinned Locked Moved .NET (Core and Framework)
databasehelpquestion
4 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.
  • J Offline
    J Offline
    Jason188
    wrote on last edited by
    #1

    :(( Is there any way in a combobox to find the index of an item or set the item in a combobox by the value? I know there is a FindString, and a FindStringExact method in the combobox object, but is there anything simmilar or any work around to getting the index of an object or setting the selecteditem by the value? Any help is appreciated, thank you. Jason

    T 1 Reply Last reply
    0
    • J Jason188

      :(( Is there any way in a combobox to find the index of an item or set the item in a combobox by the value? I know there is a FindString, and a FindStringExact method in the combobox object, but is there anything simmilar or any work around to getting the index of an object or setting the selecteditem by the value? Any help is appreciated, thank you. Jason

      T Offline
      T Offline
      Tal Kain
      wrote on last edited by
      #2

      If I understand your request You want to find the index of a value in the combo box and also getting the selected item index ? well, for getting the index of a value in the combo box: cmbBox.Items.IndexOf("Male"); will give you the index of the object with the value of "Male".. and to get the selected item index you can use the property "SelectedIndex": cmbBox.SelectedIndex to get the index...(or to set it..) and: cmbBox.SelectedText for a string or cmbBox.SelectedValue for an object (get && set) next time, make a little search on our good friend Google... You could find it easly.. Good luck.

      J 1 Reply Last reply
      0
      • T Tal Kain

        If I understand your request You want to find the index of a value in the combo box and also getting the selected item index ? well, for getting the index of a value in the combo box: cmbBox.Items.IndexOf("Male"); will give you the index of the object with the value of "Male".. and to get the selected item index you can use the property "SelectedIndex": cmbBox.SelectedIndex to get the index...(or to set it..) and: cmbBox.SelectedText for a string or cmbBox.SelectedValue for an object (get && set) next time, make a little search on our good friend Google... You could find it easly.. Good luck.

        J Offline
        J Offline
        Jason188
        wrote on last edited by
        #3

        Without googl'ing it I already knew all of those properties you are telling me about (not to be arrogant, just to answer your reference to Google, I know better). You didn't understand the question, perhaps you haven't encountered this problem, if you have a combo box loaded with objects, say STATE objects, and the objects have 5-10 properties, and this is say on a customers form, when the user loads the info on a customer, you want to set the state the customer is from, and in your database, in the customers table you will only store the state id for the customer, and thus when you populate your customers form, you will only have the state id, unfortunately even if you create a state object with just the id set, when you do an indexof it won't match and give you nothing, and like I said, you have only the state id, which means no text, and if you do cmbBox.SelectedValue = myState.Id, it won't work even if you set the value member as ID... so what I'm after is something to this effect: myComboBox.SelectedIndex = [some function that returns the index of the object in combo box with ID = x] OR myComboBox.SelectedItem= [some function that returns the object in combo box with ID = x] OR myComboBox.SelectedValue = myState.Id Jason

        T 1 Reply Last reply
        0
        • J Jason188

          Without googl'ing it I already knew all of those properties you are telling me about (not to be arrogant, just to answer your reference to Google, I know better). You didn't understand the question, perhaps you haven't encountered this problem, if you have a combo box loaded with objects, say STATE objects, and the objects have 5-10 properties, and this is say on a customers form, when the user loads the info on a customer, you want to set the state the customer is from, and in your database, in the customers table you will only store the state id for the customer, and thus when you populate your customers form, you will only have the state id, unfortunately even if you create a state object with just the id set, when you do an indexof it won't match and give you nothing, and like I said, you have only the state id, which means no text, and if you do cmbBox.SelectedValue = myState.Id, it won't work even if you set the value member as ID... so what I'm after is something to this effect: myComboBox.SelectedIndex = [some function that returns the index of the object in combo box with ID = x] OR myComboBox.SelectedItem= [some function that returns the object in combo box with ID = x] OR myComboBox.SelectedValue = myState.Id Jason

          T Offline
          T Offline
          Tal Kain
          wrote on last edited by
          #4

          So in the beginning I didn’t understand your exact problem... Anyway, At the moment the best way I think about to solve this problem is creating your own function that runs on the list of states and search for the specific id... so the function will return the index.. to make it easier You can even inherit from the class Combo Box and overload the function indexOf so it will do the job.. When I will have a better answer for this problem I will notify you :-) Good luck!

          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