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. .NET ComboBox Item Tags (SetItemData equivalent)

.NET ComboBox Item Tags (SetItemData equivalent)

Scheduled Pinned Locked Moved C#
tutorialcsharpdatabasecom
5 Posts 3 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.
  • G Offline
    G Offline
    Gaul
    wrote on last edited by
    #1

    I am trying to set tags for individual Combobox items. In looking through the Combobox doc, I cannot find the equivalent of SetItemData/GetItemData. The tag property is set at the Combobox level, and not at the item level. Can someone point me to an example, or how to set the tags for invidual combobox items. I would like to store database record ids as tags in the combobox. Gaulles Gaulles Gaulles Technologies, Inc. http://www.gaulles.com

    L 1 Reply Last reply
    0
    • G Gaul

      I am trying to set tags for individual Combobox items. In looking through the Combobox doc, I cannot find the equivalent of SetItemData/GetItemData. The tag property is set at the Combobox level, and not at the item level. Can someone point me to an example, or how to set the tags for invidual combobox items. I would like to store database record ids as tags in the combobox. Gaulles Gaulles Gaulles Technologies, Inc. http://www.gaulles.com

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Hi Do this:

      public class ComboBoxItem
      {
      object tag = null;
      string text = null;

      public ComboBoxItem(string text)
      {
      this.text = text;
      }
      public object Tag {get {return tag;} set {tag = value;}}

      public override string ToString(){return text;}
      }

      Now just add these objects to ComboBox.Items. ie

      comboBox1.Items.Add( new ComboBoxItem("some text"));

      You can set/get the tags via:

      object tag = ((ComboBoxItem) comboBox1.Items[i]).Tag; //note cast. [edit] oops made a mistake, correct now :)[/edit]

      Hope this helps :) Give them a chance! Do it for the kittens, dear God, the kittens!

      G 1 Reply Last reply
      0
      • L leppie

        Hi Do this:

        public class ComboBoxItem
        {
        object tag = null;
        string text = null;

        public ComboBoxItem(string text)
        {
        this.text = text;
        }
        public object Tag {get {return tag;} set {tag = value;}}

        public override string ToString(){return text;}
        }

        Now just add these objects to ComboBox.Items. ie

        comboBox1.Items.Add( new ComboBoxItem("some text"));

        You can set/get the tags via:

        object tag = ((ComboBoxItem) comboBox1.Items[i]).Tag; //note cast. [edit] oops made a mistake, correct now :)[/edit]

        Hope this helps :) Give them a chance! Do it for the kittens, dear God, the kittens!

        G Offline
        G Offline
        Gaul
        wrote on last edited by
        #3

        I thought of doing something similar, but because this was already part of MFC (SetItemData/GetItemData), I was digging around to see if it is already part of the .NET framework ComboBox class. I cannot understand it when MS takes away features that were already existing, and used by several applications. Thanks for the info. I will implement it.

        L J 2 Replies Last reply
        0
        • G Gaul

          I thought of doing something similar, but because this was already part of MFC (SetItemData/GetItemData), I was digging around to see if it is already part of the .NET framework ComboBox class. I cannot understand it when MS takes away features that were already existing, and used by several applications. Thanks for the info. I will implement it.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Gaul wrote: I thought of doing something similar, but because this was already part of MFC (SetItemData/GetItemData), You will ussually add an object to ComboBox.Items and then just override the ToString() method in the objects's class. Alternatively, you can display any property (obvously the ToString() method gets called on the Property's reflected type) with ComboBox.ValueMember. NOTE: however there is a bug ,according to me anyways, when the ComboBox gets sorted and retrieving SelectedItem in relation to SelectedValue. Allways makes sure the object is the same. Hope this helps :) Give them a chance! Do it for the kittens, dear God, the kittens!

          1 Reply Last reply
          0
          • G Gaul

            I thought of doing something similar, but because this was already part of MFC (SetItemData/GetItemData), I was digging around to see if it is already part of the .NET framework ComboBox class. I cannot understand it when MS takes away features that were already existing, and used by several applications. Thanks for the info. I will implement it.

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            Gaul wrote: I cannot understand it when MS takes away features that were already existing, and used by several applications. On the contrary, the functionality is still there; just how you use it is changed. Rather than following the MFC paradigm where you added strings then set related data separately; you just add the data and tell the combobox how to represent it (by setting the DisplayMember and ValueMember properties of the combobox, if DisplayMember isn't set it defaults to calling ToString()) James "And we are all men; apart from the females." - Colin Davies

            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