Winform组件【三】 重写自己的ComboBox
继前两篇文章winform 程序对界面上控件的统一控制【二】组件版和winform 程序对界面上控件的统一控制【一】(赋值清空验证……),本篇文章我要实现的是重写ComboBoxEdit(Dev控件),定义自己的数据绑定和显示样式。(donet的ComboBox控件同理可得)
先看看重写ComboBoxEdit所提出的需求:
(1)少量结构化的数据需要填充到ComboBoxEdit的Items集合中,在窗体画面上,我们看到的是"index - value" 的形式,但是真正取的值是Index。这种功能也就是donet中的ComboBox控件提供的DisplayMember和ValueMember属性,可以实现这种表现值和实际值不一样的功能。
(2)这种少量结构化数据是不存储在数据中也不存储在文件中,如果每个ComboBoxEdit控件我们都去设置Items属性,则会很麻烦, 因为这样的数据很多。
如:提供的端数处理区分数据为:5 切舍,6 四舍五入,7 进位。实现的效果如图1所示(中间加"-"是项目中规定的):
图1
图2
图3
看看我所重写的ComboBoxEdit的组件:
代码一
public partial class UCComboBox : https://www.360docs.net/doc/847369656.html,boBoxEdit
{
///
/// 组件构造函数
///
public UCComboBox()
{
InitializeComponent();
Initialize();
}
private void Initialize()
{
this.Properties.AutoComplete = true;
this.Properties.ImmediatePopup = true;
this.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
}
///
/// 构造
///
///
public UCComboBox(IContainer container)
{
container.Add(this);
InitializeComponent();
Initialize();
}
private string _itemValue = "";
///
/// ComboBoxEdit选中值对应的索引
///
[Browsable(false)]
public string ItemValue
{
get
{
ret
urn GetIndex(this._controlType.ToString(), this.Text);
}
set
{
_itemValue = value;
if (!DesignMode)
{
this.Text = this.GetComplexValueByIndex(_itemValue);
}
}
}
private string _itemDisplay = "";
///
/// ComboBoxEdit选中的值
///
[Browsable(false)]
public string ItemDisplay
{
get
{
return GetValue(this._controlType.ToString(), this.Text);
}
set { _itemDisplay = value; }
}
private CtrlType _controlType = CtrlType.默认;
///
/// 控件类型属性
///
[Description("选择控件类型"), Category("自定义")]
[DefaultValue(typeof(CtrlType), "默认")]
public CtrlType ControlType
{
get { return _controlType; }
set
{
_controlType = value;
if (!this.DesignMode)
{
if (_controlType != CtrlType.默认)
{
FillCombItem(_controlType.ToString());
}
}
}
}
private bool _emptyItem = false;
///
/// 是否包含空行
///
[Description("是否含有空行,非必录下拉框有空行,必录的下拉框不要加空行"), Category("自定义")]
[DefaultValue(typeof(bool), "false")]
public bool AEmptyIte
m
{
get
{
return _emptyItem;
}
set
{
_emptyItem = value;
if (!DesignMode && _emptyItem)
{
this.Properties.Items.Add("");
}
}
}
///
/// 填充下拉框Item
///
///
private void FillCombItem(string _ctrlname)
{
Dictionary
foreach (KeyValuePair
{
this.Properties.Items.Add(string.Format("{0} - {1}", kvp.Key, kvp.Value));
}
}
private List
///
/// 自定义枚举类型
///
[Category("自定义")]
[Browsable(true)]
[Description("特殊的枚举,自定义枚举键值"),
ReadOnlyAttribute(false)]
public List
{
get
{
return _enumlist;
}
set
{
if (_controlType == CtrlType.默认 && _enumlist.Count != 0 && !this.DesignMode)
{
_enumlist = value;
FillCombItem(_enumlist);
}
}
}
///
/// 填充下拉框Item
///
///
private
void FillCombItem(List
{
ctrlEnum.ForEach(enumitem =>
{
this.Properties.Items.Add(enumitem.EnumText);
});
}
///
/// 获取comboboxEdit当前值所对应的索引值
///
/// 控件类型
/// 当前选取值
///
private string GetIndex(string _ctrlname, string content)
{
int start = content.IndexOf("-");
try
{
if (start > 0)
{
return content.Split('-')[0].Trim();
}
else
return GetIndexByValue(content);
}
catch (System.Exception err)
{
throw err;
}
}
///
/// 获取comboboxEdit当前值所对应的值
///
/// 控件类型
/// 当前选取值
///
private string GetValue(string _ctrlname, string content)
{
int start = content.IndexOf("-");
try
{
if (start > 0)
{
return content.Split('-')[1].Trim();
}
else
return content;
}
catch (System.Exception err)
{
throw err;
}
}
///
/// 通过Enum的索引获取对应的值
/// 无对应值是返回为空
///
/// 索引
///
public string GetValueByIndex(string index)
{
try
{
foreach (string _item in this.Properties.Items)
{
int start = _item.IndexOf("-");
if (start > 0)
{
string[] arr = _item.Split('-');
if (arr[0].Trim().Equals(index))
{
return arr[1].Trim();
}
}
}
return "";
}
catch (System.Exception err)
{
throw err;
}
}
///
/// 通过Enum的索引获取对应的值,形如:1-是
/// 无对应值是返回为空
///
///
///
public string GetComplexValueByIndex(string index)
{
try
{
foreach (string _item in this.Properties.Items)
{
int start = _item.IndexOf("-");
if (start > 0)
{
string[] arr = _item.Split('-');
if (arr[0].Trim().Equals
(index))
{
return _item;
}
}
}
return "";
}
catch (System.Exception err)
{
throw err;
}
}
///
/// 获取comboboxEdit当前值所对应的索引值
/// 无对应值是返回为空
///
/// 当前选取值
///
public string GetIndexByValue(string content)
{
try
{
foreach (string _item in this.Properties.Items)
{
int start = _item.IndexOf("-");
if (start > 0)
{
string[] arr = _item.Split('-');
if (arr[1].Trim().Equals(content))
{
return arr[0].Trim();
}
}
}
return "";
}
catch (System.Exception err)
{
throw err;
}
}
///
/// 获取comboboxEdit当前值所对应的索引值
/// 无对应值是返回为空
///
/// 当前选取值,形如:1-是
///
public string GetIndexB
yComplexValue(string content)
{
try
{
int start = content.IndexOf("-");
if (start > 0)
{
return content.Split('-')[0].Trim();
}
return "";
}
catch (System.Exception err)
{
throw err;
}
}
}