扩展ComboBox,支持数据自验证及错误提示,需要另外实现数据验证逻辑类
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace WindowsApplication7
{
/// <summary>
/// ProComboBox1 的摘要说明。
/// </summary>
public class ProComboBox1 : System.Windows.Forms.ComboBox
{
public System.Windows.Forms.ErrorProvider errorProvider;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private string mapName="";
private bool realCheck; //实时验证开关
private string errorMsg=""; //错误提示信息
private string checkExpression=""; //预置的数据验证表达式
private bool checkOver; //数据验证结论
private string errorIcon;
private int dropStyle=0;
private bool sorted=false;
public ProComboBox1()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.errorProvider = new System.Windows.Forms.ErrorProvider();
//
// errorProvider
//
this.errorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.NeverBlink;
}
#endregion
#region property_block
[Category("Behavior"),Description("实时验证开关,true打开,false关闭")]
public bool RealCheck
{
get
{
return realCheck;
}
set
{
if(realCheck!=value)
{
realCheck=value;
if(realCheck)
{
this.comboBox.Validating += new System.ComponentModel.CancelEventHandler(this.comboBox_Validating);
}
else
{
this.comboBox.Validating += null;
}
}
}
}
[Category("Behavior"),Description("当前字段的初始值")]
public string OldValue
{
get
{
return oldValue;
}
set
{
if(oldValue!=value)
oldValue=value;
}
}
[Category("Behavior"),Description("设置下拉列表框的样式,0-下拉列表,1-编辑框加下拉列表,2-单一编辑框")]
public int DropStyle
{
get
{
return dropStyle;
}
set
{
if(value>=0&&value<=2)
{
dropStyle=value;
switch(dropStyle)
{
case 0:
comboBox.DropDownStyle=ComboBoxStyle.DropDownList;
break;
case 1:
comboBox.DropDownStyle=ComboBoxStyle.DropDown ;
break;
case 2:
comboBox.DropDownStyle=ComboBoxStyle.Simple ;
break;
}
}
}
}
[Category("Behavior"),Description("下拉列表项排序开关,true打开,false关闭")]
public bool Sorted
{
get
{
return sorted;
}
set
{
if(sorted!=value)
{
sorted=value;
comboBox.Sorted=sorted;
}
}
}
[Category("Behavior"),Description("错误提示信息")]
public string ErrorMsg
{
get
{
return errorMsg;
}
set
{
if(errorMsg!=value)
{
errorMsg=value;
}
}
}
[Category("Behavior"),Description("预置的数据验证表达式,数据验证控件将根据此表达式对值进行检验")]
public string CheckExpression
{
get
{
return checkExpression;
}
set
{
if(checkExpression!=value)
{
checkExpression=value;
}
}
}
[Category("Behavior"),Description("数据验证结论,此值只读")]
public bool CheckOver
{
get
{
return checkOver;
}
}
#endregion
#region method_block
protected override void OnPaint(PaintEventArgs pe)
{
// TODO: 在此添加自定义绘画代码
// 调用基类 OnPaint
base.OnPaint(pe);
}
private void comboBox_Validating(object sender, CancelEventArgs e)
{
CheckOverForExpression();
}
public void CheckOverForExpression()
{ //checkExpression是此用户控件的属性,在控件初始化时从绑定的字段的属性validexpression赋值
if(checkExpression.Trim().Length>1)
{
ShowDataValidation sdv=new ShowDataValidation();
errorMsg=sdv.CheckForExpression(null,comboBox.SelectedValue.ToString() ,oldValue ,-1 ,checkExpression);
if(errorMsg.Trim().Length>1)
errorMsg=mapName+errorMsg;
SetErrorMsg(errorMsg);
}
else
{
SetErrorMsg("");
}
}
public void SetErrorMsg(string msg)
{
if(msg.Trim().Length>1)
{
this.BackColor=System.Drawing.Color.Gold;
checkOver=false;
}
else
{
this.BackColor=System.Drawing.Color.FromName("Window");
checkOver=true;
}
errorProvider.SetError(this,msg);
}
#endregion
}
}
浙公网安备 33010602011771号