ComboBox控件值对类

 

ComboBox控件值对类 
--------------------------------------------------------------------------------

#region  ComboBox
#region  Item
///   <summary>
///  ComboBox控件手动赋值,调用示范
///  ArrayList ArrL = new ArrayList();
///  ArrL.Add(new Item("A", "a"));
///  ComboBox.DataSource = ArrL;
///  ComboBox.DisplayMember= "Text";
///  ComboBox.ValueMember = "Value";
///   </summary>
public   class  Item
{
private   string  _Text;
private   string  _Value;
public  Item( string  sText,  string  sValue)
{
this ._Text  =  sText;
this ._Value  =  sValue;
}
public   string  Text
{
get
{
return  _Text;
}
}
public   string  Value

get
{
return  _Value ;
}
}
public   override   string  ToString()
{
return   this .Text  +   "  -  "   +   this .Value;
}
}
#endregion
#region  ComboBoxValues
///   <summary>
///  输入值对字符串,返回ComboBox实例
///  调用示范:
///  ComboBoxValues cbv = new ComboBoxValues(txtSelect);
///  txtSelect = cbv.Add(ini.Read("Main","SelectText"),ini.Read("Main","SelectValue"),'*'); 
///   </summary>
public   class  ComboBoxValues
{
///   <summary>
///  构造ComboBox控件实例以便返回该类型
///   </summary>
///   <param name="cb"> ComboBox实例 </param>
public  ComboBoxValues(System.Windows.Forms.ComboBox cb)
{
_ComboBox 
=  cb;
}
private  System.Windows.Forms.ComboBox _ComboBox;
private   string  err;
///   <summary>
///  错误提示,返回 null 为正常
///   </summary>
public   string  Err
{
get
{
return  err;
}
}
///   <summary>
///  增加ComboBox新项,返回ComboBox实例
///   </summary>
///   <param name="sText"> 以分割符分开的显示文本 </param>
///   <param name="sValue"> 以分割符分开的值文本 </param>
///   <param name="key"> 分割符,如: * </param>
///   <returns></returns>
public  System.Windows.Forms.ComboBox Add( string  sText,  string  sValue,  char  key)
{
err 
=   null
string [] arrText  =  sText.Split(key);
string [] arrValue  =  sValue.Split(key);
if (arrText.Length == arrValue.Length)
{
System.Collections.ArrayList arrl 
=   new  System.Collections.ArrayList();
for ( int  i = 0 ;i < arrText.Length;i ++ )
{
arrl.Add(
new  Item(arrText[i], arrValue[i]));
}
_ComboBox.DataSource 
=  arrl;
_ComboBox.DisplayMember
=   " Text " ;
_ComboBox.ValueMember 
=   " Value " ;
}
else
{
err
= " Text与Value值对不等 " ;
}
return  _ComboBox;
}
}
#endregion
#endregion

你可能感兴趣的:(combobox)