assign value to combobox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lttan123
    New Member
    • Oct 2008
    • 11

    assign value to combobox

    hi there
    i am having some difficulties to bind value to combobox in windows application.

    combo.Items.Add (new List("a","b"))

    I have created a class.
    The value is there but what is displayed is the class name. How should i display a value from he value i have keyed in. For instance i want to display 'a'
  • vanc
    Recognized Expert New Member
    • Mar 2007
    • 211

    #2
    Code:
    combo.Items.Add("a");
    Is that what you want?

    Comment

    • lttan123
      New Member
      • Oct 2008
      • 11

      #3
      Originally posted by vanc
      Code:
      combo.Items.Add("a");
      Is that what you want?
      nope. Well i want a displayed in my combobox. But i also want to assign a value to a . for instance 1.

      so when i click on 'a' the value is 1

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by lttan123
        nope. Well i want a displayed in my combobox. But i also want to assign a value to a . for instance 1.

        so when i click on 'a' the value is 1
        You need to realize there is a separation between those two things.
        For 'a' to have a value, then 'a' is a variable.
        For you to display something in a combo box it needs to be a string.

        You may want to investigate Dictionary<>
        Basically its a 2d array. You could put the key into your combobox then use it's value elsewhere in your program.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          This is a bit of a nuisance with the Combobox. Basically the specs say you can add an object too to the combobox. The value displayed will be the ToString() of that object. So you may need to create a small class that holds the value and override it's ToString to return the display text.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Originally posted by r035198x
            This is a bit of a nuisance with the Combobox. Basically the specs say you can add an object too to the combobox. The value displayed will be the ToString() of that object. So you may need to create a small class that holds the value and override it's ToString to return the display text.
            Cool. I didn't know that one. Will have to try it some time. Thanks!

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by tlhintoq
              Cool. I didn't know that one. Will have to try it some time. Thanks!
              Like I said, the whole thing is quite a nuisance. Creating a class just so you can have name, value pairs.

              Comment

              • vanc
                Recognized Expert New Member
                • Mar 2007
                • 211

                #8
                Originally posted by lttan123
                nope. Well i want a displayed in my combobox. But i also want to assign a value to a . for instance 1.

                so when i click on 'a' the value is 1
                You can use 2 combo boxes, one to hold "a" while the other will hold "1" accordingly. Then you make IndexChanged event that will synchronize those two combo boxes, so everytime you pick "a" in the first box, the second box will be "1". Just an idea to get around it. :D

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by vanc
                  You can use 2 combo boxes, one to hold "a" while the other will hold "1" accordingly. Then you make IndexChanged event that will synchronize those two combo boxes, so everytime you pick "a" in the first box, the second box will be "1". Just an idea to get around it. :D
                  Not very efficient memory and performance wise I have to say. Especially if more than one comboboxes are required. The advantage of creating that class to hold the values is that you (normally) only need one class for all your comboboxes.

                  Comment

                  Working...