自定义ItemList类

1 篇文章 0 订阅

在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;
            }
        }
    }


 


       
       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值