zoukankan      html  css  js  c++  java
  • WinForm CheckedBoxList Add Item and Value

    最近剛好幫同事解決這個問題~把解決方式提出來講一下~

    因為CheckedBoxList.Items.Add的時候他新增的是一個object~所以往往不知道要怎麼加入他的Value~

    此時可以先寫一個Class~來實做這個Item,見以下範例


        public class myItem
        {
            String m_Text="";
            String m_Value="";

            public myItem(String Text)
            {
                m_Text = Text;
            }
            public myItem(String Text, String Value)
            {
                m_Text = Text;
                m_Value = Value;
            }

            public String Text
            {
                get { return m_Text; }
                set { m_Text = value; }
            }
            public String Value
            {
                get { return m_Value; }
                set { m_Value = value; }
            }

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

        }

    以這個class來當作CheckedBoxList的Item來新增~

    這Class最主要就是要override ToString()這個Method~跟建立Text & Value的Property

    新增方式為

    checkedListBox1.Items.Add(new myItem("11","aa"));
    checkedListBox1.Items.Add(new myItem("22","BB"));

    讀取為

    ((myItem)checkedListBox1.Items[1]).Text

    ((myItem)checkedListBox1.Items[1]).Value

    引用自 :http://www.dotblogs.com.tw/jacky19819/archive/2009/03/25/7696.aspx

  • 相关阅读:
    JSON解析之——Android
    Xml解析之——Java/Android/Python
    Design Pattern —— Singleton
    设计模式(10)--观察者模式
    设计模式(9)--建造者模式
    设计模式(8)--外观模式
    设计模式(7)--模板模式
    设计模式(6)--原型模式
    设计模式(5)--工厂模式
    设计模式(4)--代理模式
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2354242.html
Copyright © 2011-2022 走看看