博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
窗体控件的验证
阅读量:4567 次
发布时间:2019-06-08

本文共 2536 字,大约阅读时间需要 8 分钟。

自定义了一个窗体控件验证的类

///     /// 窗体控件的验证    ///     public class WinFormValidating    {        ///         /// 路径验证        ///         /// 
public static bool validtePath(Control ctrl,ErrorProvider ep) { string pattern = @"^[a-zA-Z]:(((\\(?! )[^/:*?<>\""|\\]+)+\\?)|(\\)?)\s*$"; Regex myRegex = new Regex(pattern); bool isR = myRegex.IsMatch(ctrl.Text); if (!isR) ep.SetError(ctrl, "所输入的路径不合法"); return isR; } /// /// 验证是否为空 /// /// /// ///
public static bool validateNull(Control ctrl, ErrorProvider ep) { string content = ctrl.Text; if ((content == null) || content.Trim().Length == 0) { ep.SetError(ctrl, "不能为空"); return false; } return true; } /// /// 验证窗体所有控件 /// /// public static bool validateAll(Form f1) { //有一个控件没通过验证,返回false bool isR = true; foreach (Control ctrl in f1.Controls) { ctrl.Focus(); bool tempB= f1.Validate(); if (!tempB) isR = tempB; } return isR; } /// /// 验证整数 /// /// /// ///
public static bool validateInteger(Control ctrl, ErrorProvider ep) { string context = ctrl.Text; string pattern = @"^([0-9]{1,})$"; Regex myRegex = new Regex(pattern); bool isR = myRegex.IsMatch(context); if (!isR) ep.SetError(ctrl, "所输入的不是有效的整数"); return isR; } /// /// 验证正浮点数 /// /// /// ///
public static bool validatePositiveFloat(Control ctrl,ErrorProvider ep) { string context = ctrl.Text; string pattern = @"^[1-9]d*.d*|0.d*[1-9]d*$"; Regex myRegex = new Regex(pattern); bool isR = myRegex.IsMatch(context); if (!isR) ep.SetError(ctrl, "所输入的不是有效的整数"); return isR; } }

 需要验证的文本框的验证事件示例如下:

private void txtOutPath_Validating(object sender, CancelEventArgs e)        {            bool bt = WinFormValidating.validtePath(txtOutPath,errorProvider1);            if (!bt)                e.Cancel = true;        }

 

 

 

转载于:https://www.cnblogs.com/DayDreamEveryWhere/archive/2013/04/03/2998700.html

你可能感兴趣的文章
确保新站自身站点设计的合理性的六大注意点
查看>>
promise
查看>>
Go 网络编程笔记
查看>>
[]Java面试题123道
查看>>
中间件与auth认证的那点儿所以然
查看>>
Scala
查看>>
Android 中LinearLayout控件属性
查看>>
面向对象之多态性
查看>>
树状数组
查看>>
【2019.8.14 慈溪模拟赛 T1】我不是!我没有!别瞎说啊!(notme)(BFS+DP)
查看>>
多任务--进程 及 进程间通信
查看>>
多线程/多进程+QProgressBar实现进度条
查看>>
多任务(进程)案例----- 拷贝文件夹
查看>>
Kotlin的快速入门
查看>>
底层原理
查看>>
21. Merge Two Sorted Lists
查看>>
shiro设置加密算法源码解析
查看>>
实验四
查看>>
win8.1镜像制作
查看>>
Windows 服务开发框架介绍 - Topshelf
查看>>