C# Combobox 設置 value


因為ComboxItem是Object對象,而控件自身沒有Value屬性.所以,需要自定義一個類,用其對象來存儲Text,Value.

   public class ComboxItem
    {
        private string text;
        private string values;

        public string Text
        {
            get { return this.text; }
            set { this.text = value; }
        }

        public string Values
        {
            get { return this.values; }
            set { this.values = value; }
        }

        public ComboxItem(string _Text, string _Values)
        {
            Text = _Text;
            Values = _Values;
        }


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


賦值示例一:

     cbDictData.Items.Add(new ComboxItem("用戶類型", "D1"));
            cbDictData.Items.Add(new ComboxItem("地區字典", "D2"));
            cbDictData.Items.Add(new ComboxItem("區域字典", "D3")); 
  
賦值示例二:

 ComboxItem[] values = {
                new ComboxItem("用戶類型", "D1"),
                new ComboxItem("地區字典", "D2"),
                new ComboxItem("區域字典", "D3")
            };
            cbDictData.Items.AddRange(values);

 

取值示例:

string strDict = ((ComboxItem)cbDictData.SelectedItem).Values;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM