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. Adding tag or euivenlt to combobox item?

Adding tag or euivenlt to combobox item?

Scheduled Pinned Locked Moved C#
question
9 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.
  • L Offline
    L Offline
    liqnit
    wrote on last edited by
    #1

    Hi Is it possible to to add Tag or data of some kind to a combobox item? no the whole combobox a single Item inside . Thanks

    Have a nice Day

    L C 2 Replies Last reply
    0
    • L liqnit

      Hi Is it possible to to add Tag or data of some kind to a combobox item? no the whole combobox a single Item inside . Thanks

      Have a nice Day

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, ComboBox (and ListBox) collect items of any type you choose, not just string. So you can create a special item class to hold whatever you want, and add instances of that class to your ComboBox. Now you should use ComboBox.DrawMode=OwnerDrawFixed and do the item painting yourself using the ComboBox.DrawItem event. :)

      Luc Pattyn try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }

      L 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, ComboBox (and ListBox) collect items of any type you choose, not just string. So you can create a special item class to hold whatever you want, and add instances of that class to your ComboBox. Now you should use ComboBox.DrawMode=OwnerDrawFixed and do the item painting yourself using the ComboBox.DrawItem event. :)

        Luc Pattyn try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }

        L Offline
        L Offline
        liqnit
        wrote on last edited by
        #3

        Thanks i will try is it too much to as a for a code sample...;)

        Have a nice Day

        L 1 Reply Last reply
        0
        • L liqnit

          Hi Is it possible to to add Tag or data of some kind to a combobox item? no the whole combobox a single Item inside . Thanks

          Have a nice Day

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Doesn't a combo box have a display item and data item property ? Or is that just a combo box in a web page ?

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          L 1 Reply Last reply
          0
          • C Christian Graus

            Doesn't a combo box have a display item and data item property ? Or is that just a combo box in a web page ?

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            L Offline
            L Offline
            liqnit
            wrote on last edited by
            #5

            I cant find such property only Item.

            Have a nice Day

            L 1 Reply Last reply
            0
            • L liqnit

              Thanks i will try is it too much to as a for a code sample...;)

              Have a nice Day

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Good news, you dont need OwnerDraw, it suffices to provide a ToString() method. Example:

              // the item class:
              class cbi {
              public int i;
              public string text;
              public cbi(int i, string text) { this.i=i; this.text=text; }
              public override string ToString() { return text+" "+i; }
              }

              // inside the Form class:
              public Form1() {
              InitializeComponent();
              cb.Items.Add(new cbi(1, "a"));
              cb.Items.Add(new cbi(2, "b"));
              cb.Items.Add(new cbi(3, "c"));
              cb.SelectedIndexChanged+=new EventHandler(cb_SelectedIndexChanged);
              }

              void log(string s) {Console.WriteLine(s);}

              void cb_SelectedIndexChanged(object sender, EventArgs e) {
              cbi item=(cbi)cb.SelectedItem;
              // now you can access any member/property of item:
              if(item==null) log("item=null");
              else log("item i="+item.i);
              }

              :)

              Luc Pattyn try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }

              1 Reply Last reply
              0
              • L liqnit

                I cant find such property only Item.

                Have a nice Day

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi, this is an example of the technique Christian refered to:

                class cbi {
                private int val; public int MyVal { get { return val; } }
                private string name; public string MyName { get { return name; } }

                public cbi(int i, string text) { this.val=i; this.name=text; }
                

                }

                public Form123() {
                InitializeComponent();
                ArrayList list=new ArrayList();
                list.Add(new cbi(1, "a"));
                list.Add(new cbi(2, "b"));
                list.Add(new cbi(3, "c"));
                cb.DataSource=list;
                cb.DisplayMember="MyName";
                cb.ValueMember="MyVal";
                cb.SelectedIndexChanged+=new EventHandler(cb_SelectedIndexChanged);
                }

                void log(string s) {Console.WriteLine(s);}

                void cb_SelectedIndexChanged(object sender, EventArgs e) {
                cbi item=(cbi)cb.SelectedItem;
                if(item==null) log("null");
                else {
                int i=(int)cb.SelectedValue;
                log("i="+i);
                }
                }

                It uses an ArrayList (or any other collection) as the source of all ComboBox data; each item must now have two properties, one giving the text to display, one giving the value to be returned by ComboBox.SelectedValue :)

                Luc Pattyn try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }

                L 1 Reply Last reply
                0
                • L Luc Pattyn

                  Hi, this is an example of the technique Christian refered to:

                  class cbi {
                  private int val; public int MyVal { get { return val; } }
                  private string name; public string MyName { get { return name; } }

                  public cbi(int i, string text) { this.val=i; this.name=text; }
                  

                  }

                  public Form123() {
                  InitializeComponent();
                  ArrayList list=new ArrayList();
                  list.Add(new cbi(1, "a"));
                  list.Add(new cbi(2, "b"));
                  list.Add(new cbi(3, "c"));
                  cb.DataSource=list;
                  cb.DisplayMember="MyName";
                  cb.ValueMember="MyVal";
                  cb.SelectedIndexChanged+=new EventHandler(cb_SelectedIndexChanged);
                  }

                  void log(string s) {Console.WriteLine(s);}

                  void cb_SelectedIndexChanged(object sender, EventArgs e) {
                  cbi item=(cbi)cb.SelectedItem;
                  if(item==null) log("null");
                  else {
                  int i=(int)cb.SelectedValue;
                  log("i="+i);
                  }
                  }

                  It uses an ArrayList (or any other collection) as the source of all ComboBox data; each item must now have two properties, one giving the text to display, one giving the value to be returned by ComboBox.SelectedValue :)

                  Luc Pattyn try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }

                  L Offline
                  L Offline
                  liqnit
                  wrote on last edited by
                  #8

                  Thanks a lot i am now working with the code

                  Have a nice Day

                  L 1 Reply Last reply
                  0
                  • L liqnit

                    Thanks a lot i am now working with the code

                    Have a nice Day

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    You're welcome.

                    Luc Pattyn try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }

                    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