Commit 7707ae4c HZH

验证

1 个父辈 635c46c4
......@@ -21,7 +21,8 @@ namespace HZH_Controls.Controls
InitializeComponent();
}
private Control owner;
private Control owner;
[Browsable(true), Category("自定义属性"), Description("父控件"), Localizable(true)]
public Control Owner
{
get { return owner; }
......@@ -29,11 +30,11 @@ namespace HZH_Controls.Controls
{
// The owner form cannot be set to null.
if (value == null)
throw new ArgumentNullException();
return;
// The owner form can only be set once.
if (owner != null)
throw new InvalidOperationException();
return;
// Save the form for future reference.
owner = value;
......
......@@ -369,14 +369,15 @@ namespace HZH_Controls.Controls
/// 处理 Windows 消息。
/// </summary>
/// <param name="m">一个 Windows 消息对象。</param>
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 15 || m.Msg == 7 || m.Msg == 8)
{
this.OnPaint(null);
}
}
//protected override void WndProc(ref Message m)
//{
// base.WndProc(ref m);
// if (m.Msg == 15 || m.Msg == 7 || m.Msg == 8)
// {
// //this.OnPaint(null);
// Invalidate();
// }
//}
/// <summary>
/// Handles the <see cref="E:TextChanged" /> event.
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HZH_Controls.Controls
{
public class VerificationAttribute : Attribute
{
public VerificationAttribute(string strRegex = "", string strErrorMsg = "")
{
Regex = strRegex;
ErrorMsg = strErrorMsg;
}
public string Regex { get; set; }
public string ErrorMsg { get; set; }
}
}
// ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-09-27
//
// ***********************************************************************
// <copyright file="VerificationEventArgs.cs">
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HZH_Controls.Controls
{
/// <summary>
/// Class VerificationEventArgs.
/// Implements the <see cref="System.EventArgs" />
/// </summary>
/// <seealso cref="System.EventArgs" />
public class VerificationEventArgs : EventArgs
{
/// <summary>
/// Gets or sets the verification control.
/// </summary>
/// <value>The verification control.</value>
public Control VerificationControl { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [verify success].
/// </summary>
/// <value><c>true</c> if [verify success]; otherwise, <c>false</c>.</value>
public bool IsVerifySuccess { get; set; }
/// <summary>
/// Gets or sets the verification model.
/// </summary>
/// <value>The verification model.</value>
public VerificationModel VerificationModel { get; set; }
/// <summary>
/// 是否已处理,如果为true,则不再使用默认验证提示功能
/// </summary>
/// <value><c>true</c> if this instance is processed; otherwise, <c>false</c>.</value>
public bool IsProcessed { get; set; }
/// <summary>
/// Gets or sets 正则表达式
/// </summary>
/// <value>The custom regex.</value>
public string Regex { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="VerificationEventArgs"/> is required.
/// </summary>
/// <value><c>true</c> if required; otherwise, <c>false</c>.</value>
public bool Required { get; set; }
/// <summary>
/// Gets or sets the error MSG.
/// </summary>
/// <value>The error MSG.</value>
public string ErrorMsg { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace HZH_Controls.Controls
{
/// <summary>
/// 验证规则
/// </summary>
public enum VerificationModel
{
/// <summary>
/// 无
/// </summary>
[Description("无"), VerificationAttribute()]
None = 1,
/// <summary>
/// 任意字母数字下划线
/// </summary>
[Description("任意字母数字下划线"), VerificationAttribute(@"^[a-zA-Z_0-1]*$", "请输入任意字母数字下划线")]
AnyChar = 2,
/// <summary>
/// 任意数字
/// </summary>
[Description("任意数字"), VerificationAttribute(@"^[\-\+]?\d+(\.\d+)?$", "请输入任意数字")]
Number = 4,
/// <summary>
/// 非负数
/// </summary>
[Description("非负数"), VerificationAttribute(@"^(\+)?\d+(\.\d+)?$", "请输入非负数")]
UnsignNumber = 8,
/// <summary>
/// 正数
/// </summary>
[Description("正数"), VerificationAttribute(@"(\+)?([1-9][0-9]*(\.\d{1,2})?)|(0\.\d{1,2})", "请输入正数")]
PositiveNumber = 16,
/// <summary>
/// 整数
/// </summary>
[Description("整数"), VerificationAttribute(@"^[\+\-]?\d+$", "请输入整数")]
Integer = 32,
/// <summary>
/// 非负整数
/// </summary>
[Description("非负整数"), VerificationAttribute(@"^(\+)?\d+$", "请输入非负整数")]
UnsignIntegerNumber = 64,
/// <summary>
/// 正整数
/// </summary>
[Description("正整数"), VerificationAttribute(@"^[0-9]*[1-9][0-9]*$", "请输入正整数")]
PositiveIntegerNumber = 128,
/// <summary>
/// 邮箱
/// </summary>
[Description("邮箱"), VerificationAttribute(@"^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|NET|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|int|INT)$", "请输入正确的邮箱地址")]
Email = 256,
/// <summary>
/// 手机
/// </summary>
[Description("手机"), VerificationAttribute(@"^(\+?86)?1\d{10}$", "请输入正确的手机号")]
Phone = 512,
/// <summary>
/// IP
/// </summary>
[Description("IP"), VerificationAttribute(@"(?=(\b|\D))(((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))(?=(\b|\D))", "请输入正确的IP地址")]
IP = 1024,
/// <summary>
/// Url
/// </summary>
[Description("Url"), VerificationAttribute(@"^[a-zA-z]+://(//w+(-//w+)*)(//.(//w+(-//w+)*))*(//?//S*)?$", "请输入正确的网址")]
URL = 2048,
/// <summary>
/// 身份证号
/// </summary>
[Description("身份证号"), VerificationAttribute(@"^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$", "请输入正确的身份证号")]
IDCardNo = 4096,
/// <summary>
/// 正则验证
/// </summary>
[Description("自定义正则表达式"), VerificationAttribute()]
Custom = 8192,
}
}
......@@ -140,11 +140,11 @@ namespace HZH_Controls.Forms
Color _foreColor = m_foreColor == null ? Color.White : m_foreColor.Value;
System.Drawing.SizeF sizeText = g.MeasureString(strMsg, _font);
g.Dispose();
var formSize = new Size((int)sizeText.Width + 20, (int)sizeText.Height + 20);
if (formSize.Width < 20)
formSize.Width = 20;
if (formSize.Height < 20)
formSize.Height = 20;
var formSize = new Size((int)sizeText.Width + 10, (int)sizeText.Height + 10);
if (formSize.Width < 10)
formSize.Width = 10;
if (formSize.Height < 10)
formSize.Height = 10;
if (m_location == AnchorTipsLocation.LEFT || m_location == AnchorTipsLocation.RIGHT)
{
formSize.Width += 20;
......@@ -244,7 +244,7 @@ namespace HZH_Controls.Forms
Graphics gBit = Graphics.FromImage(bit);
gBit.SetGDIHigh();
gBit.FillPath(new SolidBrush(_background), path);
gBit.DrawString(strMsg, _font, new SolidBrush(_foreColor), rect.Location + new Size(10, 10));
gBit.DrawString(strMsg, _font, new SolidBrush(_foreColor), rect, new StringFormat() { Alignment= StringAlignment.Center, LineAlignment= StringAlignment.Center});
gBit.Dispose();
#endregion
......@@ -264,6 +264,7 @@ namespace HZH_Controls.Forms
/// <param name="deviation">The deviation.</param>
/// <param name="fontSize">Size of the font.</param>
/// <param name="autoCloseTime">The automatic close time.</param>
/// <param name="blnTopMost">是否置顶</param>
/// <returns>FrmAnchorTips.</returns>
public static FrmAnchorTips ShowTips(
Control parentControl,
......@@ -273,7 +274,8 @@ namespace HZH_Controls.Forms
Color? foreColor = null,
Size? deviation = null,
int fontSize = 10,
int autoCloseTime = 5000)
int autoCloseTime = 5000,
bool blnTopMost=true)
{
Point p;
if (parentControl is Form)
......@@ -288,7 +290,7 @@ namespace HZH_Controls.Forms
{
p = p + deviation.Value;
}
return ShowTips(new Rectangle(p, parentControl.Size), strMsg, location, background, foreColor, fontSize, autoCloseTime);
return ShowTips(new Rectangle(p, parentControl.Size), strMsg, location, background, foreColor, fontSize, autoCloseTime, parentControl.FindForm(),blnTopMost);
}
#endregion
......@@ -303,6 +305,8 @@ namespace HZH_Controls.Forms
/// <param name="foreColor">Color of the fore.</param>
/// <param name="fontSize">Size of the font.</param>
/// <param name="autoCloseTime">The automatic close time.</param>
/// <param name="parentForm">父窗体</param>
/// <param name="blnTopMost">是否置顶</param>
/// <returns>FrmAnchorTips.</returns>
public static FrmAnchorTips ShowTips(
Rectangle rectControl,
......@@ -311,11 +315,13 @@ namespace HZH_Controls.Forms
Color? background = null,
Color? foreColor = null,
int fontSize = 10,
int autoCloseTime = 5000)
int autoCloseTime = 5000,
Form parentForm = null,
bool blnTopMost = true)
{
FrmAnchorTips frm = new FrmAnchorTips(rectControl, strMsg, location, background, foreColor, fontSize, autoCloseTime);
frm.TopMost = true;
frm.Show();
frm.TopMost = blnTopMost;
frm.Show(parentForm);
return frm;
}
#endregion
......
......@@ -21,6 +21,7 @@ using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HZH_Controls.Controls;
namespace HZH_Controls.Forms
{
......
......@@ -85,6 +85,12 @@
<Compile Include="Controls\ScrollBar\UCHScrollbar.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="Controls\Verification\VerificationAttribute.cs" />
<Compile Include="Controls\Verification\VerificationComponent.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Controls\Verification\VerificationEventArgs.cs" />
<Compile Include="Controls\Verification\VerificationModel.cs" />
<Compile Include="Forms\FrmLoading.cs">
<SubType>Form</SubType>
</Compile>
......
......@@ -26,6 +26,7 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using HZH_Controls.Controls;
namespace HZH_Controls
{
......
......@@ -60,6 +60,7 @@ namespace Test
tnControl.Nodes.Add("图标");
tnControl.Nodes.Add("滚动条");
tnControl.Nodes.Add("控件水印");
tnControl.Nodes.Add("表单验证");
this.tvMenu.Nodes.Add(tnControl);
TreeNode tnCharts = new TreeNode(" 图表");
......@@ -246,6 +247,9 @@ namespace Test
case "控件水印":
AddControl(new UC.UCTestGraphicalOverlay());
break;
case "表单验证":
AddControl(new UC.UCTestVerification() { Dock = DockStyle.Fill });
break;
#endregion
#region 图表 English:Chart
......
......@@ -302,6 +302,12 @@
<Compile Include="UC\UCTestValve.Designer.cs">
<DependentUpon>UCTestValve.cs</DependentUpon>
</Compile>
<Compile Include="UC\UCTestVerification.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="UC\UCTestVerification.Designer.cs">
<DependentUpon>UCTestVerification.cs</DependentUpon>
</Compile>
<Compile Include="UC\UCTestWave.cs">
<SubType>UserControl</SubType>
</Compile>
......@@ -449,6 +455,9 @@
<EmbeddedResource Include="UC\UCTestValve.resx">
<DependentUpon>UCTestValve.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="UC\UCTestVerification.resx">
<DependentUpon>UCTestVerification.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="UC\UCTestWave.resx">
<DependentUpon>UCTestWave.cs</DependentUpon>
</EmbeddedResource>
......
......@@ -21,9 +21,9 @@ namespace Test.UC
private void graphicalOverlay1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SetGDIHigh();
if (blnColor)
{
e.Graphics.SetGDIHigh();
foreach (Control item in this.Controls)
{
if (item is Button)
......
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HZH_Controls.Controls;
using HZH_Controls;
using System.Drawing.Drawing2D;
namespace Test.UC
{
public partial class UCTestVerification : UserControl
{
public UCTestVerification()
{
InitializeComponent();
}
List<Control> lstErrorControl = new List<Control>();
private void button1_Click(object sender, EventArgs e)
{
lstErrorControl.Clear();
this.verificationComponent1.Verification();
this.panel1.Invalidate(true);
}
private void UCTestVerification_Load(object sender, EventArgs e)
{
List<KeyValuePair<string, string>> lstCom = new List<KeyValuePair<string, string>>();
for (int i = 0; i < 5; i++)
{
lstCom.Add(new KeyValuePair<string, string>(i.ToString(), "选项" + i));
}
this.ucComboBox1.Source = lstCom;
this.ucComboBox2.Source = lstCom;
this.ucCombox2.Source = lstCom;
this.ucCombox1.Source = lstCom;
List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "ID", HeadText = "编号", Width = 70, WidthType = SizeType.Absolute });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Name", HeadText = "姓名", Width = 100, WidthType = SizeType.Absolute });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Age", HeadText = "年龄", Width = 100, WidthType = SizeType.Absolute });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Birthday", HeadText = "生日", Width = 120, WidthType = SizeType.Absolute, Format = (a) => { return ((DateTime)a).ToString("yyyy-MM-dd"); } });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Sex", HeadText = "性别", Width = 100, WidthType = SizeType.Absolute, Format = (a) => { return ((int)a) == 0 ? "女" : "男"; } });
this.ucComboxGrid1.GridColumns = lstCulumns;
List<object> lstSourceGrid = new List<object>();
for (int i = 0; i < 100; i++)
{
TestGridModel model = new TestGridModel()
{
ID = i.ToString(),
Age = 3 * i,
Name = "姓名——" + i,
Birthday = DateTime.Now.AddYears(-10),
Sex = i % 2
};
lstSourceGrid.Add(model);
}
this.ucComboxGrid1.GridDataSource = lstSourceGrid;
ucComboxGrid2.GridDataSource = lstSourceGrid;
}
private void graphicalOverlay1_Paint(object sender, PaintEventArgs e)
{
if (lstErrorControl.Count > 0)
{
e.Graphics.SetGDIHigh();
foreach (Control control in lstErrorControl)
{
if (control.Parent == this.groupBox2 || control.Parent == this.groupBox4)
{
var p = control.Location;
p.Offset(control.Parent.Location);
GraphicsPath path = ControlHelper.CreateRoundedRectanglePath(new Rectangle(p.X - 1, p.Y - 1, control.Width + 2, control.Height + 2), 4);
e.Graphics.DrawPath(new Pen(new SolidBrush(Color.FromArgb(100, 255, 0, 0)), 2), path);
}
}
}
}
private void verificationComponent1_Verificationed(VerificationEventArgs e)
{
if (!e.IsVerifySuccess)
{
lstErrorControl.Add(e.VerificationControl);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="verificationComponent1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="graphicalOverlay1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>212, 17</value>
</metadata>
</root>
\ No newline at end of file
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!