Winforms中的验证文本框 [英] Validation textbox in winforms

查看:50
本文介绍了Winforms中的验证文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Winforms的文本框控件中验证文本?
我有一个控件,用户必须在其中放置字符串,例如"13:55".我想显示MessageBox,当此值与"XX:YY"不同时.
怎么做?
在asp.net中,创建起来非常容易,但是如何在winforms上实现它呢?

How to validate text in textbox control in winforms?
I have a control, where user have to put string, like "13:55". I want to show MessageBox, when this value will be diffrent, than "XX:YY".
How to do it?
In asp.net it was so easy to make, but how to implement it on winforms?

推荐答案

查看

Check out the MaskedTextBox if you don't want to have to validate in the first place.

var l_control = new MaskedTextBox();
l_control.Mask = "00\:00";

如果要使第一位为可选:

If you want to make the first digit optional:

l_control.Mask = "90\:90";

否则,您可以使用正则表达式.用冒号分隔的4位数字为: @"^ \ d {2}:\ d {2} $" .( @ 符号可防止C#将'\'当作转义字符-正则表达式没有唯一的含义.)

Otherwise, you could use a regular expression. 4 digits separated by a colon would be: @"^\d{2}:\d{2}$". (The @ symbol prevents C# from treating '\' as an escape character - nothing unique to regex.)

这篇关于Winforms中的验证文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆