自定义ItemList类

来源:互联网 发布:淘宝店铺导航条长度 编辑:程序博客网 时间:2024/06/06 03:24

在ASP.NET中System.Web.UI.WebControls.ListItem可以方便的在DropDownList控件中添加数据。

    public partial class DropDownListDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ListItem li;
            for (int i = 0; i < 4; i++)
            {
                li = new ListItem(i.ToString(), "2");
                DropDownList1.Items.Add(li);
            }
        }
    }

但在桌面应用程序中没有对应的类为 ComboBox 和 DataGridViewTextBoxColumn 添加数据,则没有相应 ListItem 类。
为了在桌面应用程序中进行类似操作需要自己定义功能和 ListItem 相似的类 Item 类。


ComboBox 功能简介

ComboBox 控件常用属性:
数据
  DataBindings
    SelectedItem          //返回所选择的Add添加或DataSource邦定的对象
    SelectedText          //此属性,始终返回为""
    SelectedValue         //返回所选择的ValueMember中的值,如果是输入或者没有邦定ValueMember的都回返回错误。
    Tag                   //一般用于存放对象
    Text                  //可以正常获取
  DataSource
  DisplayMember
  Items
  Tag
  ValueMember
外观
  DropDownStyle
    Simple              //下拉平铺,可以填写,可以选择
    DropDown            //可以填写,可以选择
    DropDownList        //只能选择

ComboBox的简单用法:
            comboBox1.Items.Add("Text");
            comboBox1.Items.Add(new Item("Text", "Value"));

ComboBox.Items.Add(new Item("Text", "Value"));
功能是将Item通过Item.ToString()方法转换成字符串,然后作为Add()的参数处理。


ComboBox的绑定数据源用法:
            ArrayList DataSource = new ArrayList();
            DataSource.Add(new Item("Text", "Value"));
            DataSource.Add(new Item("Text", "Value"));
            DataSource.Add(new Item("Text", "Value"));
            DataSource.Add(new Item("Text", "Value"));

            comboBox1.DataSource = DataSource;
            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Value";

这里Item的定义是关键,DisplayMember和ValueMember绑定的"Text"和 "Value"在Item中是两个属性。
    namespace List
    {

        public class Item
        {
            private string text;
            private string value;

            public Item(string text, string value)
            {

                this.text = text;
                this.value = value;
            }

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

            public string Value
            {

                get
                {
                    return this.value;
                }
                set
                {
                    this.value = value;
                }
            }

            public override string ToString()
            {
                return this.text + " - " + this.value;
            }

        }
    }

ComboBox的自定义绑定数据源用法:
    ItemList itemList = new ItemList();
    itemList.Add(new Item("Text", "Value"));
    itemList.Add(new Item("Text", "Value"));
    itemList.Add(new Item("Text", "Value"));
    itemList.Add(new Item("Text", "Value"));


    comboBox1.DataSource = itemList;
    comboBox1.DisplayMember = "Text";
    comboBox1.ValueMember = "Value";

这里ItemCollection 的定义是关键,ItemList是Item的集合类。
ItemCollection 采用和 ArrayList 一样的接口 IList, ICollection, IEnumerable, ICloneable。
相当于将 ArrayList 重写了一遍。


    ItemCollection itemList =new ItemCollection ();
    itemList.Add(new Item("Text", "Value"));
    itemList.Add(new Item("Text", "Value"));
    itemList.Add(new Item("Text", "Value"));
    itemList.Add(new Item("Text", "Value"));
    foreach (Item item in ItemCollection )
    {
        string Text = item.ToString();
    }

ItemCollection 类
    using System;
    using System.Collections;
    namespace List
    {

        public class ItemCollection : IList, ICollection, IEnumerable, ICloneable
        {
            ArrayList item = new ArrayList();
            int currentIndex;
            public ItemCollection()
            {
                currentIndex = 0;
            }
            public object this[int index]
            {
                get
                {
                    if (index > -1 && index < item.Count)
                    {
                        return (Item)item[index];
                    }
                    else
                    {
                        return null;
                    }
                }
                set
                {
                    if (index > -1 && index < item.Count)
                    {
                        item[index] = value;
                    }
                    else
                    {
                        item.Add(value);
                    }
                }
            }
            public bool IsFixedSize
            {
                get
                {
                    return item.IsFixedSize;
                }
            }
            public bool IsReadOnly
            {
                get
                {
                    return item.IsReadOnly;
                }
            }
            public int Count
            {
                get
                {
                    return item.Count;
                }
            }
            public object Current
            {
                get
                {
                    return item[currentIndex];
                }
            }
            /// <summary>
            /// 获取一个值,该值指示是否同步对 ItemList 的访问(线程安全)。
            /// </summary>
            public bool IsSynchronized
            {
                get
                {
                    return item.IsSynchronized;
                }
            }
            /// <summary>
            /// 获取可用于同步 ItemList 访问的对象。
            /// </summary>
            public object SyncRoot
            {
                get
                {
                    return item;
                }
            }
            public object Clone()
            {
                return item.Clone();
            }
            public void Insert(int index, object value)
            {
                item.Insert(index, value);
            }
            public int Add(Item value)
            {
                return item.Add(value);
            }
            public int Add(object value)
            {
                return item.Add(value);
            }
            public void AddRange(ICollection c)
            {
                item.AddRange(c);
            }
            /// <summary>
            /// 从特定的 Array 索引处开始,将 ItemList 的元素复制到一个 Array 中。
            /// </summary>
            /// <param name="array"></param>
            /// <param name="arrayIndex"></param>
            public void CopyTo(Array array, int arrayIndex)
            {
                item.CopyTo(array, arrayIndex);
            }
            public bool Contains(object value)
            {
                return item.Contains(value);
            }
            public void Remove(object value)
            {
                item.Remove(value);
            }
            public void RemoveAt(int index)
            {
                item.RemoveAt(index);
            }
            public int IndexOf(object value)
            {
                return item.IndexOf(value);
            }
            public void Clear()
            {
                item.Clear();
            }
            public IEnumerator GetEnumerator()
            {
                return item.GetEnumerator();
            }
            public IEnumerator GetEnumerator(int index, int count)
            {
                return item.GetEnumerator(index, count);
            }
            public bool MoveNext()
            {
                if (currentIndex < item.Count)
                {
                    currentIndex++;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            public void Reset()
            {
                currentIndex = 0;
            }
        }
    }


 


       
       

原创粉丝点击