BaoKai_202508-Wms-Jingwang..../WMS.Ctrl/MisCtrlTextBox.cs
2025-08-24 09:35:55 +08:00

412 lines
14 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace WMS.Ctrl
{
public partial class WMSCtrlTextBox : DevExpress.XtraEditors.TextEdit
{
public WMSCtrlTextBox()
{
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
this.Leave += new System.EventHandler(this.textBox1_Leave);
this.TextChanged += new EventHandler(WMSCtrlTextBox_TextChanged);
this.Font = new System.Drawing.Font("宋体", 10);
this.Properties.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Flat;
}
public WMSCtrlTextBox(IContainer container)
{
container.Add(this);
this.TextChanged += new EventHandler(WMSCtrlTextBox_TextChanged);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
this.Leave += new System.EventHandler(this.textBox1_Leave);
}
public void WMSCtrlTextBox_TextChanged(object sender, EventArgs e)
{
if (mustText)
{
if (this.Text.Trim().Length > 0)
{
this.BackColor = System.Drawing.SystemColors.Window;
}
else
{ this.BackColor = System.Drawing.Color.PeachPuff; }
}
}
ValidationAttribute valatt;
[Description("正则表达式")]
[Browsable(true)]
public ValidationAttribute Valatt
{
get { return valatt; }
set { valatt = value; }
}
private bool mustText = false;
private string _valText = string.Empty;
[Description("默认值")]
[Browsable(true)]
public string ValText
{
get
{
return _valText;
}
set
{
_valText = value;
}
}
[Description("是否为必填项")]
[Browsable(true)]
public bool MustText
{
get {
return mustText;
}
set
{
mustText = value;
if (mustText)
{
this.BackColor = System.Drawing.Color.PeachPuff;
}
else
{
this.BackColor = System.Drawing.SystemColors.Window;
}
}
}
//private void GetValResult()
//{
// string msg = "";
// if (!Check(this.Text, ref msg))
// {
// MessageBox.Show(valatt + "输入错误!");
// this.Text = "";
// this.Focus();
// }
//}
private bool frmSelect=false;
[Description("是否是通用查询")]
[Browsable(true)]
public bool FrmSelect
{
get {
return frmSelect;
}
set
{
frmSelect=value;
}
}
#region
string pattern;
public bool Check(string str, ref string msg)
{
switch (valatt)
{
//case ValidationAttribute.无://无
// return true;
// break;
case ValidationAttribute.://整数
msg = "整数";
pattern = @"^-?[0-9]\d*$";
break;
case ValidationAttribute.://正整数
msg = "正整数";
pattern = "^[0-9]*[1-9][0-9]*$";
break;
case ValidationAttribute.://负整数
msg = "负整数";
pattern = "^-[0-9]*[1-9][0-9]*$";
break;
case ValidationAttribute.://数字
msg = "数字";
pattern = "^([+-]?)\\d*\\.?\\d+$";
break;
case ValidationAttribute.://电话
msg = "电话";
//在做项目时常常用到判断电话号码的正则表达式写了一个可验证如下27种格式
//110
//8888888
//88888888
//8888888-123
//88888888-23435
//0871-8888888-123
//023-88888888-23435
//86-0871-8888888-123
//8888888_123
//88888888_23435
//0871_8888888_123
//023_88888888_23435
//86_0871_8888888_123
//8888888123
//8888888823435
//08718888888123
//0238888888823435
//8608718888888123
//8888888—123
//88888888—23435
//0871—8888888—123
//023—88888888—23435
//86—0871—8888888—123
//13588888888
//15988888888
//013588888888
//015988888888
//(0315)7663551
pattern = @"((^(\d{2,4}[-_—]?)?\d{3,8}([-_—]?\d{3,8})?([-_—]?\d{1,7})?$)|(^0?1[35]\d{9}$)z)|(^(\([0-9]+\))?[0-9]{7,8}$)";
break;
case ValidationAttribute.://正数(正整数+ 0
msg = "正数(正整数+ 0";
pattern = @"^\d+$";
break;
case ValidationAttribute.://负数(负整数+ 0
msg = "负数(负整数+ 0";
pattern = @"^((-\d+)|(0+))$";
break;
case ValidationAttribute.://浮点数
msg = "浮点数";
pattern = @"^(-?\d+)(\.\d+)?$";
break;
case ValidationAttribute.://正浮点数
msg = "正浮点数";
pattern = @"^(?:[1-9][0-9]*\.[0-9]+|0\.(?!0+$)[0-9]+)$";
break;
case ValidationAttribute.://负浮点数
msg = "负浮点数";
pattern = @"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
break;
case ValidationAttribute.2://浮点数
msg = "浮点数";
pattern = @"(^\d+$)|(^(?:[1-9][0-9]*\.[0-9]+|0\.(?!0+$)[0-9]+)$)";
break;
case ValidationAttribute.://非负浮点数(正浮点数+ 0
msg = "非负浮点数(正浮点数+ 0";
pattern = @"^\d+(\.\d+)?$";
break;
case ValidationAttribute.://非正浮点数(负浮点数+ 0
msg = "非正浮点数(负浮点数+ 0";
pattern = @"^((-\d+(\.\d+)?)|(0+(\.0+)?))$";
break;
case ValidationAttribute.://邮件 //正确
msg = "邮件";
pattern = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
break;
case ValidationAttribute.://颜色
msg = "颜色";
pattern = "^[a-fA-F0-9]{6}$";
break;
case ValidationAttribute.url://url(http格式的)
msg = "url(http格式的)";
pattern = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
break;
case ValidationAttribute.://仅中文
msg = "中文";
pattern = @"[\u4e00-\u9fa5]";
break;
case ValidationAttribute.ACSII字符://仅ACSII字符
msg = "ACSII字符";
pattern = "^[\\x00-\\xFF]+$";
break;
case ValidationAttribute.://邮编
msg = "邮编";
pattern = "^\\d{6}$";
break;
case ValidationAttribute.://手机
msg = "手机";
pattern = "^[1]+[3,5]+\\d{9}";
break;
case ValidationAttribute.IP地址://ip地址
msg = "IP地址";
pattern = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
break;
case ValidationAttribute.://非空
msg = "非空";
pattern = "^\\S+$";
break;
case ValidationAttribute.://图片
msg = "图片";
pattern = @"(.*)\.(jpg|gif|png|bmp)$";
break;
case ValidationAttribute.://压缩文件
msg = "压缩文件";
pattern = "(.*)\\.(rar|zip|7zip|tgz)$";
break;
case ValidationAttribute.://日期
msg = "日期";
//这个日期正则表达式支持
//YYYY-MM-DD
//YYYY/MM/DD
//YYYY_MM_DD
//YYYY.MM.DD的形式
pattern = @"((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$))";
break;
case ValidationAttribute.QQ号码://QQ号码
msg = "QQ号码";
pattern = "[1-9][0-9]{4,}";
break;
case ValidationAttribute.://用来用户注册。匹配由数字、个英文字母或者下划线组成的字符串
msg = "正数(正整数+ 0";
pattern = @"(^\d+$)|(^(?:[1-9][0-9]*\.[0-9]+|0\.(?!0+$)[0-9]+)$)";
break;
case ValidationAttribute.://字母
msg = "字母";
pattern = "^[A-Za-z]+$";
break;
case ValidationAttribute.://大写字母
msg = "大写字母";
pattern = "^[A-Z]+$";
break;
case ValidationAttribute.://小写字母
msg = "小写字母";
pattern = "^[a-z]+$";
break;
case ValidationAttribute.://身份证
msg = "身份证";
//pattern = @"^[1-9]([0-9]{16}|[0-9]{13})[xX0-9]$";
pattern = "(^\\d{18}$)|(^\\d{15}$)";
break;
default:
pattern = string.Empty;
break;
}
return Regex.IsMatch(str, pattern);
}
#endregion
int strlen;
[Description("设置文本长度")]
[Browsable(true)]
public int StrLen
{
get { return strlen; }
set { strlen = value; }
}
public void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (GetValResult())
{
SendKeys.Send("{TAB}");
}
}
}
public void textBox1_Leave(object sender, EventArgs e)
{
GetValResult();
if (this.Text.Trim().Length == 0 && _valText =="0")
{
this.Text = _valText;
}
}
private bool GetValResult()
{
if (StrLen != 0 && this.Text.Length > StrLen)
{
MessageBox.Show("文本长度不能大于" + StrLen.ToString() + "");
this.Text = "";
this.Focus();
return false;
}
string msg = "";
if (this.Text.Trim() != "")
{
bool valBl=Check(this.Text, ref msg);
if (!valBl)
{
MessageBox.Show(msg + "输入错误!");
this.Text = "";
this.Focus();
return false;
}
}
if (mustText)
{
if (this.Text.Trim().Length > 0)
{
this.BackColor = System.Drawing.SystemColors.Window;
}
else
{
this.BackColor = System.Drawing.Color.AntiqueWhite;
}
}
return true;
}
}
public enum ValidationAttribute
{
,
,
,
,
,
,
,
,
,
,
,
,
2,
,
,
,
,
url,
,
ACSII字符,
,
,
IP地址,
,
,
,
,
QQ号码,
= 27,
,
,
,
}
}