How does combobox know key/value?

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

    #1

    How does combobox know key/value?

    When using the .Add() method of a combo box, how does it know which
    element in my object is the key and which is the value?

    Say my object looks like this:

    object1.field1 = "2";
    object1.field2 = "Number2";

    and I do:
    combobox1.Items .Add(object1);

    I'd like field2 displayed in the combo box and then some way to
    reference its corresponding value. Will I need to create a look up
    table to acheive this because field2 is all that will ever be returned
    by the combo?

    DataSource() won't be used here.

    Thanks,
    Brett

  • Luc E. Mistiaen

    #2
    Re: How does combobox know key/value?

    try combox1.Display Member = "field2" ; a similar field exist for value.

    /LM

    "Brett Romero" <account@cygen. com> wrote in message
    news:1140215154 .872522.223900@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    > When using the .Add() method of a combo box, how does it know which
    > element in my object is the key and which is the value?
    >
    > Say my object looks like this:
    >
    > object1.field1 = "2";
    > object1.field2 = "Number2";
    >
    > and I do:
    > combobox1.Items .Add(object1);
    >
    > I'd like field2 displayed in the combo box and then some way to
    > reference its corresponding value. Will I need to create a look up
    > table to acheive this because field2 is all that will ever be returned
    > by the combo?
    >
    > DataSource() won't be used here.
    >
    > Thanks,
    > Brett
    >[/color]


    Comment

    • Brett Romero

      #3
      Re: How does combobox know key/value?

      I'm asking how does the combo box know when only this syntax is used:

      combobox1.Items .Add(object1);

      Or does it actually have a key/value at that point?

      Brett

      Comment

      • Nick Malik [Microsoft]

        #4
        Re: How does combobox know key/value?

        Combobox will use the ToString call on our object to determine what value to
        display. If you define a ToString method in your object to override the
        default ToString method, then you can make sure that Combobox will display
        the field you'd like. When an item is selected, you can use the selection
        index to retrieve the actual object from the combobox.
        HTH

        --
        --- Nick Malik [Microsoft]
        MCSD, CFPS, Certified Scrummaster
        http://blogs.msdn.com/nickmalik

        Disclaimer: Opinions expressed in this forum are my own, and not
        representative of my employer.
        I do not answer questions on behalf of my employer. I'm just a
        programmer helping programmers.
        --
        "Brett Romero" <account@cygen. com> wrote in message
        news:1140215154 .872522.223900@ o13g2000cwo.goo glegroups.com.. .[color=blue]
        > When using the .Add() method of a combo box, how does it know which
        > element in my object is the key and which is the value?
        >
        > Say my object looks like this:
        >
        > object1.field1 = "2";
        > object1.field2 = "Number2";
        >
        > and I do:
        > combobox1.Items .Add(object1);
        >
        > I'd like field2 displayed in the combo box and then some way to
        > reference its corresponding value. Will I need to create a look up
        > table to acheive this because field2 is all that will ever be returned
        > by the combo?
        >
        > DataSource() won't be used here.
        >
        > Thanks,
        > Brett
        >[/color]


        Comment

        • Brett Romero

          #5
          Re: How does combobox know key/value?

          Once I retrieve my object with the two fields via the selection index,
          I have to always cast it back to what ever it was to get at those two
          fields right?

          Brett

          Comment

          • Luc E. Mistiaen

            #6
            Re: How does combobox know key/value?

            It use Reflection and the DisplayMember you specify to know what to display

            /LM

            "Brett Romero" <account@cygen. com> wrote in message
            news:1140245421 .763849.167780@ g43g2000cwa.goo glegroups.com.. .[color=blue]
            > I'm asking how does the combo box know when only this syntax is used:
            >
            > combobox1.Items .Add(object1);
            >
            > Or does it actually have a key/value at that point?
            >
            > Brett
            >[/color]


            Comment

            • Brett Romero

              #7
              Re: How does combobox know key/value?

              Not so Luc. When you use the .Add() method, Display and Value members
              are not used. They are used only with datasource.

              Brett

              Comment

              • Nick Malik [Microsoft]

                #8
                Re: How does combobox know key/value?

                Yes. The combobox stores objects and uses the ToString method to get the
                displayable portion. You can put any object you want in a listbox or
                combobox, as long as you can produce a string for the list to display. When
                you get the object back our, you will need to cast it back to its original
                type in order to directly access the fields in your code.

                --
                --- Nick Malik [Microsoft]
                MCSD, CFPS, Certified Scrummaster
                http://blogs.msdn.com/nickmalik

                Disclaimer: Opinions expressed in this forum are my own, and not
                representative of my employer.
                I do not answer questions on behalf of my employer. I'm just a
                programmer helping programmers.
                --
                "Brett Romero" <account@cygen. com> wrote in message
                news:1140284604 .413815.66930@f 14g2000cwb.goog legroups.com...[color=blue]
                > Once I retrieve my object with the two fields via the selection index,
                > I have to always cast it back to what ever it was to get at those two
                > fields right?
                >
                > Brett
                >[/color]


                Comment

                • Brett Romero

                  #9
                  Re: How does combobox know key/value?

                  Right. Now, as for the ToString() method, which of the two fields will
                  it choose to send back if both are string fields? There must be some
                  logic going on there to decide field1 or field2. Does it matter if
                  the two fields in my object are int and string fields. Both of them
                  are also displayable. Which will ToString() send back now?

                  Brett

                  Comment

                  • Nick Malik [Microsoft]

                    #10
                    Re: How does combobox know key/value?

                    "Brett Romero" <account@cygen. com> wrote in message
                    news:1140299031 .160455.97310@g 14g2000cwa.goog legroups.com...[color=blue]
                    > Right. Now, as for the ToString() method, which of the two fields will
                    > it choose to send back if both are string fields? There must be some
                    > logic going on there to decide field1 or field2. Does it matter if
                    > the two fields in my object are int and string fields. Both of them
                    > are also displayable. Which will ToString() send back now?
                    >
                    > Brett
                    >[/color]

                    Here comes the power of object oriented design... the system doesn't
                    decide... YOU DO.

                    You override the ToString method with your own. You pick the one to
                    display. You can even create a string that combines the values... whatever
                    you want.

                    --
                    --- Nick Malik [Microsoft]
                    MCSD, CFPS, Certified Scrummaster
                    http://blogs.msdn.com/nickmalik

                    Disclaimer: Opinions expressed in this forum are my own, and not
                    representative of my employer.
                    I do not answer questions on behalf of my employer. I'm just a
                    programmer helping programmers.
                    --


                    Comment

                    • Luc E. Mistiaen

                      #11
                      Re: How does combobox know key/value?

                      Did you actually tried it?

                      I know, because I use it often like that.You just be sure to fill the
                      DisplayMember after you add your items.

                      /LM

                      "Brett Romero" <account@cygen. com> wrote in message
                      news:1140288409 .199320.85760@g 14g2000cwa.goog legroups.com...[color=blue]
                      > Not so Luc. When you use the .Add() method, Display and Value members
                      > are not used. They are used only with datasource.
                      >
                      > Brett
                      >[/color]


                      Comment

                      • Luc E. Mistiaen

                        #12
                        Re: How does combobox know key/value?

                        And the same effect can be achieved by having the DisplayMember point to an
                        accessor in your object that return the string you want to show.

                        /LM

                        "Nick Malik [Microsoft]" <nickmalik@hotm ail.nospam.com> wrote in message
                        news:seWdnRghV7 uQVmrenZ2dnUVZ_ sadnZ2d@comcast .com...[color=blue]
                        > "Brett Romero" <account@cygen. com> wrote in message
                        > news:1140299031 .160455.97310@g 14g2000cwa.goog legroups.com...[color=green]
                        >> Right. Now, as for the ToString() method, which of the two fields will
                        >> it choose to send back if both are string fields? There must be some
                        >> logic going on there to decide field1 or field2. Does it matter if
                        >> the two fields in my object are int and string fields. Both of them
                        >> are also displayable. Which will ToString() send back now?
                        >>
                        >> Brett
                        >>[/color]
                        >
                        > Here comes the power of object oriented design... the system doesn't
                        > decide... YOU DO.
                        >
                        > You override the ToString method with your own. You pick the one to
                        > display. You can even create a string that combines the values...
                        > whatever you want.
                        >
                        > --
                        > --- Nick Malik [Microsoft]
                        > MCSD, CFPS, Certified Scrummaster
                        > http://blogs.msdn.com/nickmalik
                        >
                        > Disclaimer: Opinions expressed in this forum are my own, and not
                        > representative of my employer.
                        > I do not answer questions on behalf of my employer. I'm just a
                        > programmer helping programmers.
                        > --
                        >
                        >[/color]


                        Comment

                        • Otis Mukinfus

                          #13
                          Re: How does combobox know key/value?

                          On 18 Feb 2006 13:43:51 -0800, "Brett Romero" <account@cygen. com>
                          wrote:
                          [color=blue]
                          >Right. Now, as for the ToString() method, which of the two fields will
                          >it choose to send back if both are string fields? There must be some
                          >logic going on there to decide field1 or field2. Does it matter if
                          >the two fields in my object are int and string fields. Both of them
                          >are also displayable. Which will ToString() send back now?
                          >
                          >Brett[/color]

                          You will have to write code to get back what you want.

                          Sometimes a picture is worth a thousand words.

                          Try something like this, Brett:

                          private void MainForm_Load(o bject sender, EventArgs e)
                          {
                          // People is inherited from List<T>
                          PeopleList people = new PeopleList();

                          // add some person objects to the list
                          people.Add(new Person(1, "Fonda", "Hanoi", "Jane"));
                          people.Add(new Person(2, "Kerry", "John", "F"));

                          // bind the list to the list box
                          LearningListBox .DataSource = people;
                          }

                          private void LearningListBox _SelectedIndexC hanged(object sender,
                          EventArgs e)
                          {
                          // get the person object from the selected item
                          Person person = (Person)Learnin gListBox.Select edItem;
                          // get the data from the person object and use it the way you want
                          int id = person.ID;
                          string lastName = person.LastName ;
                          string firstName = person.FirstNam e;
                          string middleName = person.MiddleNa me;
                          }

                          // here is the Person.ToString () method

                          public override string ToString()
                          {
                          return string.Format(" {0}, {1} {2}", _current.lastNa me,
                          _current.firstN ame, _current.middle Name);
                          }

                          Otis Mukinfus


                          Comment

                          Working...