CSharp对ListBox重写.docx

上传人:牧羊曲112 文档编号:3154867 上传时间:2023-03-11 格式:DOCX 页数:12 大小:38.89KB
返回 下载 相关 举报
CSharp对ListBox重写.docx_第1页
第1页 / 共12页
CSharp对ListBox重写.docx_第2页
第2页 / 共12页
CSharp对ListBox重写.docx_第3页
第3页 / 共12页
CSharp对ListBox重写.docx_第4页
第4页 / 共12页
CSharp对ListBox重写.docx_第5页
第5页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《CSharp对ListBox重写.docx》由会员分享,可在线阅读,更多相关《CSharp对ListBox重写.docx(12页珍藏版)》请在三一办公上搜索。

1、CSharp对ListBox重写WinForms中重写ListBox控件 /* *使用时 simListBox1.ItemCollection.Add(1, 百里屠苏); simListBox1.ItemCollection.Add(2, 风晴雪); simListBox1.ItemCollection.Add(3, 方兰生); simListBox1.ItemCollection.Add(4, 欧阳少恭); simListBox1.ItemCollection.Add(5, 工长君); simListBox1.ItemCollection.Add(6, 古剑奇谭); simListBox1.

2、SelectionMode = SelectionMode.MultiExtended;/默认值是:只能选择一项 在按钮的单击事件中: private void button1_Click(object sender, EventArgs e) for (int i = 0; i simListBox1.SelectedListBoxItems.Count; i+) /查看选择的所有项的显示内容 MessageBox.Show(simListBox1.SelectedListBoxItemsi.Key.ToString); MessageBox.Show(simListBox1.Selecte

3、dListBoxItemsi.Item.ToString); */ SimListBox控件源代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace DoorControl / / 专门用于填充ListBox信息方法 / public partial class SimListBox : Li

4、stBox /AutoScaleMode AutoScaleMode;请在SimListBox.designer.cs中注释掉 /this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; public SimListBox InitializeComponent; private DataSet _dataSet = null; / / 设置数据源 / public DataSet DataSet set _dataSet = value; private string _keyName; / / 设置加载数据第一列名称,默认为

5、第一列索引 / public string KeyName set _keyName = value; get return _keyName; private string _keyValue; / / 设置加载数据第二列名称,默认为第二列索引 / public string KeyValue set _keyValue = value; get return _keyValue; / / 获取或设置 SimListBox 中当前选定的项 / public ListBoxItem SelectedListBoxItem get int num1 = this.SelectedIndex; i

6、f (num1 != -1) return this.ItemCollectionnum1; return null; / / 获取 SimListBox 中当前选定的项 的集合 / public List SelectedListBoxItems get List temp = new List; for (int i = 0; i this.SelectedIndices.Count; i+) temp.Add(this.ItemCollectionthis.SelectedIndicesi); return temp; private ItemCollection _itemsColle

7、ction; / / 取得项目集合 / /Browsable(false) public ItemCollection ItemCollection get if (_itemsCollection = null) _itemsCollection = new ItemCollection(this); return _itemsCollection; / / 执行加载ListBox信息方法 / public new void DataBindings Binding; / / 执行加载ListBox信息方法 / / 设置数据源 public new void DataBindings(Dat

8、aSet dataSet) this._dataSet = dataSet; Binding; / / 执行加载ListBox信息方法 / / 数据集 / 键名称 / 键值 public new void DataBindings(DataSet dataSet, string keyName, string keyValue) this._dataSet = dataSet; this._keyName = keyName; this._keyValue = keyValue; Binding; / / 绑定数据项 / private void Binding if (_dataSet =

9、null) return; if (_dataSet.Tables0.Rows.Count 0) foreach (DataRow row in _dataSet.Tables0.Rows) object row1 = null; object row2 = null; if (string.IsNullOrEmpty(_keyName) row1 = row0; else row1 = row_keyName; if (string.IsNullOrEmpty(_keyValue) row2 = row1; else row2 = row_keyValue; ItemCollection.A

10、dd(row1, row2); / / 代表 ItemCollection 中项的集合 / public class ItemCollection : List private ListBox owner; / / 初始化ItemsCollection的新实例 / / 拥有该对System.Windows.Forms.ListBox public ItemCollection(ListBox owner) this.owner = owner; public ItemCollection / / 向任务集中增加一个ListBoxItem对象 / / ListBoxItem对象 public n

11、ew void Add(ListBoxItem _listBoxItem) this.owner.Items.Add(_listBoxItem.Item); base.Add(_listBoxItem); / / 向任务集中增加一个SimListBox对象 / / 键 象集合的 / 值 public void Add(object key, object item) ListBoxItem listBoxItem = new ListBoxItem(key, item); this.owner.Items.Add(item); base.Add(listBoxItem); / / 向任务集中增

12、加一个ListBoxItem对象数组 / / ListBoxItem对象数组 public void AddRange(ListBoxItem _listBoxItem) if (_listBoxItem = null) return; if (_listBoxItem.Length = 0) return; for (int i = 0; i _listBoxItem.Length; i+) this.owner.Items.Add(_listBoxItemi.Item); base.Add(_listBoxItemi); / / 向任务集中增加一个ListBoxItem对象数组 / / 键

13、数组 / 值数组 public void AddRange(object _key, object item) if (_key = null | item = null) return; if (_key.Length = 0 | item.Length = 0) return; if (_key.Length != item.Length) return; for (int i = 0; i _key.Length; i+) this.owner.Items.Add(itemi); base.Add(new ListBoxItem(_keyi, itemi); / / 从 SimListB

14、ox 中移除所有项 / public new void Clear this.owner.Items.Clear; base.Clear; / / 检索指定的项在集合中的索引 / / 一个对象,它表示要在集合中查找的项 / 项在集合内从零开始的索引;否则为 -1 public new int IndexOf(ListBoxItem value) if (value = null) return -1; return base.IndexOf(value); / / 移除 SimListBox 中指定索引处的项 / / 要移除的项的索引 public new void RemoveAt(int

15、index) if (this.owner = null) return; if (this.owner.Items.Count = 0) return; if (index = this.Count) return; this.owner.Items.RemoveAt(index); base.RemoveAt(index); / / 从 ItemCollection 中移除指定的项 / / 要从列表移除的 ListBoxItem public new void Remove(ListBoxItem value) int index = this.IndexOf(value); if (in

16、dex != -1) base.RemoveAt(index); public new ListBoxItem thisint index set baseindex = value; get return baseindex; / / ListBoxItem属性:表示ListBox中的一项 / Serializable public class ListBoxItem private object _key; / / 键,类似于表中的ID / public object Key set _key = value; get return _key; private object _item; / / 值,类似于表中的Name / public object Item set _item = value; get return _item; / / ListBoxItem属性构造函数 / / 键 / 值 public ListBoxItem(object key, object item) this._key = key; this._item = item; public ListBoxItem

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号