|
|
using DevExpress.XtraEditors;
|
|
|
using DevExpress.XtraEditors.DXErrorProvider;
|
|
|
using System.ComponentModel;
|
|
|
using System.Text.RegularExpressions;
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
namespace ProductionSystem_UserControl.CompositeControls
|
|
|
{
|
|
|
public partial class MyTextEdit : TextEdit
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 声明委托
|
|
|
/// </summary>
|
|
|
/// <param name="checkResult"></param>
|
|
|
public delegate void TransDelegate(bool checkResult);
|
|
|
|
|
|
/// <summary>
|
|
|
/// 声明事件
|
|
|
/// </summary>
|
|
|
public event TransDelegate TransEvent;
|
|
|
|
|
|
public MyTextEdit()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
//验证处理事件
|
|
|
this.Validating += new CancelEventHandler(txtValidation_Validating);
|
|
|
|
|
|
}
|
|
|
|
|
|
private string _checkRegexErrorMsg = "数据格式有误!";
|
|
|
private bool _checkIsEmpty = false;
|
|
|
private string _checkIsEmptyErrorMsg = "文本框不能为空!";
|
|
|
private string _regexString = "";
|
|
|
|
|
|
#region 自定义属性
|
|
|
/// <summary>
|
|
|
/// 验证用的表达式,默认是空的,即不验证。
|
|
|
/// </summary>
|
|
|
[Category("自定义属性"), Description("验证用的字符串表达式,默认是空的,即不验证。")]
|
|
|
public string RegexString
|
|
|
{
|
|
|
get { return _regexString; }
|
|
|
set { _regexString = value; }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 错误消息
|
|
|
/// </summary>
|
|
|
[Category("自定义属性"), Description("用于设置使用正则表达式验证发生错误时显示的消息")]
|
|
|
public string CheckRegexErrorMsg
|
|
|
{
|
|
|
get { return _checkRegexErrorMsg; }
|
|
|
set { _checkRegexErrorMsg = value; }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 是否验证文本框是空值,默认是不验证。
|
|
|
/// </summary>
|
|
|
[Category("自定义属性"), Description("是否验证文本框是空值,默认是不验证。")]
|
|
|
public bool CheckIsEmpty
|
|
|
{
|
|
|
get { return _checkIsEmpty; }
|
|
|
set { _checkIsEmpty = value; }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 当验证文本框是空值时,显示的错误信息
|
|
|
/// </summary>
|
|
|
[Category("自定义属性"), Description("验证为空的文本框显示的错误消息")]
|
|
|
public string CheckIsEmptyErrorMsg
|
|
|
{
|
|
|
get { return _checkIsEmptyErrorMsg; }
|
|
|
set { _checkIsEmptyErrorMsg = value; }
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
private void txtValidation_Validating(object sender, CancelEventArgs e)
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(this.Text))
|
|
|
{
|
|
|
if (_checkIsEmpty) //要求非空验证
|
|
|
{
|
|
|
txtErrorProvider.SetError(this, _checkIsEmptyErrorMsg, ErrorType.Critical);
|
|
|
// 设置验证控件,验证图标位置
|
|
|
txtErrorProvider.SetIconAlignment(this, ErrorIconAlignment.MiddleRight);
|
|
|
if (TransEvent != null)
|
|
|
{
|
|
|
TransEvent(false);
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
txtErrorProvider.SetError(this, "");
|
|
|
if (TransEvent != null)
|
|
|
{
|
|
|
TransEvent(true);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if (!string.IsNullOrWhiteSpace(_regexString))
|
|
|
{
|
|
|
if (!Regex.IsMatch(this.Text, _regexString))
|
|
|
{
|
|
|
txtErrorProvider.SetError(this, _checkRegexErrorMsg, ErrorType.Critical);
|
|
|
// 设置验证控件,验证图标位置
|
|
|
txtErrorProvider.SetIconAlignment(this, ErrorIconAlignment.MiddleRight);
|
|
|
if (TransEvent != null)
|
|
|
{
|
|
|
TransEvent(false);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
txtErrorProvider.SetError(this, "");
|
|
|
if (TransEvent != null)
|
|
|
{
|
|
|
TransEvent(true);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
txtErrorProvider.SetError(this, "");
|
|
|
if (TransEvent != null)
|
|
|
{
|
|
|
TransEvent(true);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 返回当前文本框控件的当前错误描述字符串。
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public string GetErrorMessage()
|
|
|
{
|
|
|
return txtErrorProvider.GetError(this);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 当用户按下Enter键或者是向下的方向键时,将控件的焦点移走。
|
|
|
/// 用户按下向上方向键时,将控件的焦点移到上一个控件。
|
|
|
/// </summary>
|
|
|
/// <param name="sender"></param>
|
|
|
/// <param name="e"></param>
|
|
|
private void TextEditValidation_KeyDown(object sender, KeyEventArgs e)
|
|
|
{
|
|
|
Form controlForm;
|
|
|
//当用户按下Enter键或者是向下的方向键时,将控件的焦点移走
|
|
|
switch (e.KeyCode)
|
|
|
{
|
|
|
case Keys.Tab:
|
|
|
{
|
|
|
controlForm = FindForm();
|
|
|
controlForm?.SelectNextControl(this, true, false, true, true);
|
|
|
break;
|
|
|
}
|
|
|
case Keys.Up: // 用户按下向上方向键时,将控件的焦点移到上一个控件。
|
|
|
{
|
|
|
controlForm = FindForm();
|
|
|
controlForm?.SelectNextControl(this, false, false, true, true);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|