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.SelectionMode=SelectionMode.MultiExtended;//默认值是:只能选择一项在按钮的单击事件中:privatevoidbutton1_Click(objectsender,EventArgse){for(inti=0;isimListBox1.SelectedListBoxItems.Count;i++){//查看选择的所有项的显示内容MessageBox.Show(simListBox1.SelectedListBoxItems[i].Key.ToString());MessageBox.Show(simListBox1.SelectedListBoxItems[i].Item.ToString());}}*/SimListBox控件源代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Text;usingSystem.Windows.Forms;namespaceDoorControl{///summary///专门用于填充ListBox信息方法////summarypublicpartialclassSimListBox:ListBox{//AutoScaleModeAutoScaleMode;请在SimListBox.designer.cs中注释掉//this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;publicSimListBox(){InitializeComponent();}privateDataSet_dataSet=null;///summary///设置数据源////summarypublicDataSetDataSet{set{_dataSet=value;}}privatestring_keyName;///summary///设置加载数据第一列名称,默认为第一列索引////summarypublicstringKeyName{set{_keyName=value;}get{return_keyName;}}privatestring_keyValue;///summary///设置加载数据第二列名称,默认为第二列索引////summarypublicstringKeyValue{set{_keyValue=value;}get{return_keyValue;}}///summary///获取或设置SimListBox中当前选定的项////summarypublicListBoxItemSelectedListBoxItem{get{intnum1=this.SelectedIndex;if(num1!=-1){returnthis.ItemCollection[num1];}returnnull;}}///summary///获取SimListBox中当前选定的项的集合////summarypublicListListBoxItemSelectedListBoxItems{get{ListListBoxItemtemp=newListListBoxItem();for(inti=0;ithis.SelectedIndices.Count;i++){temp.Add(this.ItemCollection[this.SelectedIndices[i]]);}returntemp;}}privateItemCollection_itemsCollection;///summary///取得项目集合////summary//[Browsable(false)]publicItemCollectionItemCollection{get{if(_itemsCollection==null)_itemsCollection=newItemCollection(this);return_itemsCollection;}}///summary///执行加载ListBox信息方法////summarypublicnewvoidDataBindings(){Binding();}///summary///执行加载ListBox信息方法////summary///paramname=dataSet设置数据源/parampublicnewvoidDataBindings(DataSetdataSet){this._dataSet=dataSet;Binding();}///summary///执行加载ListBox信息方法////summary///paramname=dataSet数据集/param///paramname=keyName键名称/param///paramname=keyValue键值/parampublicnewvoidDataBindings(DataSetdataSet,stringkeyName,stringkeyValue){this._dataSet=dataSet;this._keyName=keyName;this._keyValue=keyValue;Binding();}///summary///绑定数据项////summaryprivatevoidBinding(){if(_dataSet==null)return;if(_dataSet.Tables[0].Rows.Count0){foreach(DataRowrowin_dataSet.Tables[0].Rows){objectrow1=null;objectrow2=null;if(string.IsNullOrEmpty(_keyName))row1=row[0];elserow1=row[_keyName];if(string.IsNullOrEmpty(_keyValue))row2=row[1];elserow2=row[_keyValue];ItemCollection.Add(row1,row2);}}}}///summary///代表ItemCollection中项的集合////summarypublicclassItemCollection:ListListBoxItem{privateListBoxowner;///summary///初始化ItemsCollection的新实例////summary///paramname=owner拥有该对象集合的System.Windows.Forms.ListBox/parampublicItemCollection(ListBoxowner){this.owner=owner;}publicItemCollection(){}///summary///向任务集中增加一个ListBoxItem对象////summary///paramname=_listBoxItemListBoxItem对象/parampublicnewvoidAdd(ListBoxItem_listBoxItem){this.owner.Items.Add(_listBoxItem.Item);base.Add(_listBoxItem);}///summary///向任务集中增加一个SimListBox对象////summary///paramname=key键/param///paramname=item值/parampublicvoidAdd(objectkey,objectitem){ListBoxItemlistBoxItem=newListBoxItem(key,item);this.owner.Items.Add(item);base.Add(listBoxItem);}///summary///向任务集中增加一个ListBoxItem对象数组////summary///paramname=_listBoxItemListBoxItem对象数组/parampublicvoidAddRange(ListBoxItem[]_listBoxItem){if(_listBoxItem==null)return;if(_listBoxItem.Length=0)return;for(inti=0;i_listBoxItem.Length;i++){this.owner.Items.Add(_listBoxItem[i].Item);base.Add(_listBoxItem[i]);}}///summary///向任务集中增加一个ListBoxItem对象数组////summary///paramname=_key键数组/param///paramname=item值数组/parampublicvoidAddRange(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(inti=0;i_key.Length;i++){this.owner.Items.Add(item[i]);base.Add((newListBoxItem(_key[i],item[i])));}}///summary///从SimListBox中移除所有项////summarypublicnewvoidClear(){this.owner.Items.Clear();base.Clear();}///summary///检索指定的项在集合中的索引////summary///paramname=value一个对象,它表示要在集合中查找的项/param///returns项在集合内从零开始的索引;否则为-1/returnspublicnewintIndexOf(ListBoxItemvalue){if(value==null)return-1;returnbase.IndexOf(value);}///summary///移除SimListBox中指定索引处的项////summary///paramname=index要移除的项的索引/parampublicnewvoidRemoveAt(intindex){if(this.owner==null)return;if(this.owner.Items.Count=0)return;if((index0)||(index=this.Count))return;this.owner.Items.RemoveAt(index);base.RemoveAt(index);}///summary///从It