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. C#
  4. combobox item values?

combobox item values?

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
7 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.
  • D Offline
    D Offline
    dazinith
    wrote on last edited by
    #1

    i am currently doing something like the following to a combobox:

    (for int i = 0; i < 10; i++)
    {
    MyObject objData = MyData.GetAt(i);
    this.cbMyComboBox.Items.Add(objData.strWord);
    }

    which adds the text to the combobox fine.. but what i really wanna do is set a data value for each item i add.. there are 'tag' values for tree controls, but i dont see anything like this for the combobox.. and if i just add the object then it displays the object type because it doesnt know what text to display.. im sure this is a common problem, just couldn't find the answer in the message boards.. thanks in advance! still a newb.. cut me some slack :P -dz

    R 1 Reply Last reply
    0
    • D dazinith

      i am currently doing something like the following to a combobox:

      (for int i = 0; i < 10; i++)
      {
      MyObject objData = MyData.GetAt(i);
      this.cbMyComboBox.Items.Add(objData.strWord);
      }

      which adds the text to the combobox fine.. but what i really wanna do is set a data value for each item i add.. there are 'tag' values for tree controls, but i dont see anything like this for the combobox.. and if i just add the object then it displays the object type because it doesnt know what text to display.. im sure this is a common problem, just couldn't find the answer in the message boards.. thanks in advance! still a newb.. cut me some slack :P -dz

      R Offline
      R Offline
      RB Emphasys
      wrote on last edited by
      #2

      well one thing you could do is override the tostring method, and throw your own variable in there that would add some meaningful text, have you tried that? Ryan

      D 1 Reply Last reply
      0
      • R RB Emphasys

        well one thing you could do is override the tostring method, and throw your own variable in there that would add some meaningful text, have you tried that? Ryan

        D Offline
        D Offline
        dazinith
        wrote on last edited by
        #3

        that would require me writing a class derived from combobox that was familiar with class i was adding to the combobox.. so it would work, but only for one type of class.. i dont understand why there is no tag property for the items.. c++ would be like:

        for (int i = 0; i < MySet.GetSize(); i++)
        {
        MyObject* pObj = MySet.GetAt(i);
        int nPos = MyComboBox.Add(pObj->m_strText);
        MyComboBox.SetDataPtr(nPos, pObj);
        }

        that way when i handled the selection changing i could have a pointer instantly to the item it was changed to.. ah well.. still a newb.. cut me some slack :P -dz

        R 1 Reply Last reply
        0
        • D dazinith

          that would require me writing a class derived from combobox that was familiar with class i was adding to the combobox.. so it would work, but only for one type of class.. i dont understand why there is no tag property for the items.. c++ would be like:

          for (int i = 0; i < MySet.GetSize(); i++)
          {
          MyObject* pObj = MySet.GetAt(i);
          int nPos = MyComboBox.Add(pObj->m_strText);
          MyComboBox.SetDataPtr(nPos, pObj);
          }

          that way when i handled the selection changing i could have a pointer instantly to the item it was changed to.. ah well.. still a newb.. cut me some slack :P -dz

          R Offline
          R Offline
          RB Emphasys
          wrote on last edited by
          #4

          if i understand correctly, your trying to get text from objects that you add into the combo box. All objects in c# support the tostring method, it is a method of the base class, so you wouldnt have to do any extra inheritance.

          D 2 Replies Last reply
          0
          • R RB Emphasys

            if i understand correctly, your trying to get text from objects that you add into the combo box. All objects in c# support the tostring method, it is a method of the base class, so you wouldnt have to do any extra inheritance.

            D Offline
            D Offline
            dazinith
            wrote on last edited by
            #5

            AAAAAH.. i see what your saying.. :) sorry im a bit slow sometimes.. i guess that makes perfect sense, thanks! still a newb.. cut me some slack :P -dz

            1 Reply Last reply
            0
            • R RB Emphasys

              if i understand correctly, your trying to get text from objects that you add into the combo box. All objects in c# support the tostring method, it is a method of the base class, so you wouldnt have to do any extra inheritance.

              D Offline
              D Offline
              dazinith
              wrote on last edited by
              #6

              well, i thought this would work, but it doesnt..

              this.cbCountyDefault.BeginUpdate();
              for (int i = 0; i < m_saCountyRecords.Count; i++)
              {
              GASTCountyRecord objRecord = (GASTCountyRecord)m_saCountyRecords[i];
              this.cbCountyDefault.Items.Add(objRecord);
              }

              this.cbCountyDefault.SelectedIndex = 0;
              this.cbCountyDefault.EndUpdate();

              MessageBox.Show(cbCountyDefault.SelectedItem.ToString());
              MessageBox.Show(((GASTCountyRecord)this.cbCountyDefault.SelectedItem).ToString());

              the first messagebox shows the objects name, and the one where i cast it shows the corect name.. the listing in the combobox lists the class name because it populates this by calling Object.ToString() not by casting it to my object type then calling ToString().. any other suggestions? still a newb.. cut me some slack :P -dz

              D 1 Reply Last reply
              0
              • D dazinith

                well, i thought this would work, but it doesnt..

                this.cbCountyDefault.BeginUpdate();
                for (int i = 0; i < m_saCountyRecords.Count; i++)
                {
                GASTCountyRecord objRecord = (GASTCountyRecord)m_saCountyRecords[i];
                this.cbCountyDefault.Items.Add(objRecord);
                }

                this.cbCountyDefault.SelectedIndex = 0;
                this.cbCountyDefault.EndUpdate();

                MessageBox.Show(cbCountyDefault.SelectedItem.ToString());
                MessageBox.Show(((GASTCountyRecord)this.cbCountyDefault.SelectedItem).ToString());

                the first messagebox shows the objects name, and the one where i cast it shows the corect name.. the listing in the combobox lists the class name because it populates this by calling Object.ToString() not by casting it to my object type then calling ToString().. any other suggestions? still a newb.. cut me some slack :P -dz

                D Offline
                D Offline
                dazinith
                wrote on last edited by
                #7

                hrm.. it was how i was doing it.. ill put this here for future reference how not to do it:

                public class GASTCountyRecord : object
                {
                	public string m\_strCode;
                	public string m\_strName;
                
                	public GASTCountyRecord(string strCode, string strName)
                	{
                		m\_strCode = strCode;
                		m\_strName = strName;
                	}
                
                	public string ToString()
                	{	return m\_strName;	}
                }
                

                how to do it:

                public class GASTCountyRecord : object
                {
                	public string m\_strCode;
                	public string m\_strName;
                
                	public GASTCountyRecord(string strCode, string strName)
                	{
                		m\_strCode = strCode;
                		m\_strName = strName;
                	}
                
                	public **override** string ToString()
                	{	return m\_strName;	}
                }
                

                thanks for all your help.. i think the way they have you implement this is a bit wack tho still a newb.. cut me some slack :P -dz

                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