Combobox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan T

    Combobox

    Can I load the items into combobox with id too?
    Eg.
    Apple with id 1
    Orange with id 2
    Lemon with id 3


  • Stanimir Stoyanov

    #2
    RE: Combobox

    "Alan T" wrote:
    Can I load the items into combobox with id too?
    Eg.
    Apple with id 1
    Orange with id 2
    Lemon with id 3
    Hello Alan,

    Items in the ComboBox control can be instances of any class therefore you
    can create one for your needs, for instance, "ItemInfo." It would have a Text
    and ID properties which you can assign values to and later use them to
    manipulate other objects. Note that you will have to override the ToString()
    method and return a meaningful value which will appear in the ComboBox's list.

    I hope this helps.
    --
    Best regards,

    Stanimir Stoyanov
    admin@nospam.st oyanoff.info

    Comment

    • AnduAlem

      #3
      RE: Combobox

      here is a sample

      public class ComboBoxItem
      {
      public ComboBoxItem(st ring displayText, string itemValue)
      {
      this.ItemValue = itemValue;
      this.DisplayTex t = displayText;
      }

      private string _itemValue;
      private string _displayText;

      public string DisplayText
      {
      get
      {
      return _displayText;
      }
      set
      {
      _displayText = value;
      }
      }

      public string ItemValue
      {
      get
      {
      return _itemValue;
      }
      set
      {
      _itemValue = value;
      }
      }
      }


      to bind the combobox with your custom values you would do this ...

      ComboBoxItem[] dataSourceItems = {new ComboBoxItem("o ne", "1"),new
      ComboBoxItem("t wo", "2"),new ComboBoxItem("t hree", "3")};

      this.comboBox1. ValueMember = "DisplayTex t";
      this.comboBox1. DisplayMember = "ItemValue" ;

      this.comboBox1. DataSource = dataSourceItems ;


      Note that the class above is created for ilustration purposes and that it
      can be any other class and you can bind the combobox to any other object and
      all you have to do change the properties that will be used for ValueMember &
      DisplayMember.


      I hope this will be helpful

      A

      "Alan T" wrote:
      Can I load the items into combobox with id too?
      Eg.
      Apple with id 1
      Orange with id 2
      Lemon with id 3
      >
      >
      >

      Comment

      • Alan T

        #4
        Re: Combobox

        I have 10 instances of a class and would like to add these 10 instances into
        the combobox, then how do I do that?
        Do I really need to pack these 10 instances into an array ?

        "AnduAlem" <AnduAlem@discu ssions.microsof t.comwrote in message
        news:0E04192B-AAD5-4B3C-B5EE-D0D1217E6FD2@mi crosoft.com...
        here is a sample
        >
        public class ComboBoxItem
        {
        public ComboBoxItem(st ring displayText, string itemValue)
        {
        this.ItemValue = itemValue;
        this.DisplayTex t = displayText;
        }
        >
        private string _itemValue;
        private string _displayText;
        >
        public string DisplayText
        {
        get
        {
        return _displayText;
        }
        set
        {
        _displayText = value;
        }
        }
        >
        public string ItemValue
        {
        get
        {
        return _itemValue;
        }
        set
        {
        _itemValue = value;
        }
        }
        }
        >
        >
        to bind the combobox with your custom values you would do this ...
        >
        ComboBoxItem[] dataSourceItems = {new ComboBoxItem("o ne", "1"),new
        ComboBoxItem("t wo", "2"),new ComboBoxItem("t hree", "3")};
        >
        this.comboBox1. ValueMember = "DisplayTex t";
        this.comboBox1. DisplayMember = "ItemValue" ;
        >
        this.comboBox1. DataSource = dataSourceItems ;
        >
        >
        Note that the class above is created for ilustration purposes and that it
        can be any other class and you can bind the combobox to any other object
        and
        all you have to do change the properties that will be used for ValueMember
        &
        DisplayMember.
        >
        >
        I hope this will be helpful
        >
        A
        >
        "Alan T" wrote:
        >
        >Can I load the items into combobox with id too?
        >Eg.
        >Apple with id 1
        >Orange with id 2
        >Lemon with id 3
        >>
        >>
        >>

        Comment

        Working...