CSharp对ComboBox重写

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

WinForms中重写ComboBox控件/***使用时simComboBox1.ItemsCollection.Add(2,姜云凡);simComboBox1.ItemsCollection.Add(4,唐雨柔);simComboBox1.ItemsCollection.Add(6,龙幽);simComboBox1.ItemsCollection.Add(8,小蛮);simComboBox1.ItemsCollection.Add(9,姚仙);simComboBox1.ItemsCollection.Add(10,仙剑五);simComboBox1.DropDownStyle=ComboBoxStyle.DropDownList;在按钮的单击事件中:privatevoidbutton1_Click(objectsender,EventArgse){if(simComboBox1.SelectedIndex==-1){MessageBox.Show(选中项的索引是-1);return;}MessageBox.Show(simComboBox1.SelectedComboBoxItem.Key.ToString());MessageBox.Show(simComboBox1.SelectedComboBoxItem.Item.ToString());}*/SimComboBox控件源代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Text;usingSystem.Windows.Forms;namespaceDoorControl{///summary///继承ComboBox控件,方便使用ComboBox控件///使用方式类似于WebForm的DropDownList////summarypublicpartialclassSimComboBox:ComboBox{publicSimComboBox(){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///获取或设置SimComboBox中当前选定的项////summarypublicComboBoxItemSelectedComboBoxItem{get{intnum1=this.SelectedIndex;if(num1!=-1){returnthis.ItemsCollection[num1];}returnnull;}}privateItemsCollection_itemsCollection;///summary///取得项目集合////summary//[Browsable(false)]publicItemsCollectionItemsCollection{get{if(_itemsCollection==null)_itemsCollection=newItemsCollection(this);return_itemsCollection;}}///summary///执行加载ComboBox信息方法////summarypublicnewvoidDataBindings(){Binding();}///summary///执行加载ComboBox信息方法////summary///paramname=dataSet设置数据源/parampublicnewvoidDataBindings(DataSetdataSet){this._dataSet=dataSet;Binding();}///summary///执行加载ComboBox信息方法////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];ItemsCollection.Add(row1,row2);}}}}///summary///代表ItemsCollection中项的集合////summarypublicclassItemsCollection:ListComboBoxItem{privateComboBoxowner;///summary///初始化ItemsCollection的新实例////summary///paramname=owner拥有该对象集合的System.Windows.Forms.ComboBox/parampublicItemsCollection(ComboBoxowner){this.owner=owner;}publicItemsCollection(){}///summary///向任务集中增加一个ComboBoxItem对象////summary///paramname=_comboBoxItemComboBoxItem对象/parampublicnewvoidAdd(ComboBoxItem_comboBoxItem){this.owner.Items.Add(_comboBoxItem.Item);base.Add(_comboBoxItem);}///summary///向任务集中增加一个SimComboBox对象////summary///paramname=key键/param///paramname=item值/parampublicvoidAdd(objectkey,objectitem){ComboBoxItemcomboBoxItem=newComboBoxItem(key,item);this.owner.Items.Add(item);base.Add(comboBoxItem);}///summary///向任务集中增加一个ComboBoxItem对象数组////summary///paramname=_comboBoxItemComboBoxItem对象数组/parampublicvoidAddRange(ComboBoxItem[]_comboBoxItem){if(_comboBoxItem==null)return;if(_comboBoxItem.Length=0)return;for(inti=0;i_comboBoxItem.Length;i++){this.owner.Items.Add(_comboBoxItem[i].Item);base.Add(_comboBoxItem[i]);}}///summary///向任务集中增加一个ComboBoxItem对象数组////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((newComboBoxItem(_key[i],item[i])));}}///summary///从SimComboBox中移除所有项////summarypublicnewvoidClear(){this.owner.Items.Clear();base.Clear();}///summary///检索指定的项在集合中的索引////summary///paramname=value一个对象,它表示要在集合中查找的项/param///returns项在集合内从零开始的索引;否则为-1/returnspublicnewintIndexOf(ComboBoxItemvalue){if(value==null)return-1;returnbase.IndexOf(value);}///summary///移除SimComboBox中指定索引处的项////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///从ItemsCollection中移除指定的项////summary///paramname=value要从列表移除的ComboBoxItem/parampublicnewvoidRemove(ComboBoxItemvalue){intindex=this.IndexOf(value);if(index!=-1){base.RemoveAt(index);}}publicnewComboBoxItemthis[intindex]{set{base[index]=value;}get{returnbase[index];}}}///summary///ComboBoxItem

1 / 9
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功