使用C#验证文本框 [英] validate a textbox using C#

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

问题描述

大家晚上好............
我只需要一个代码来验证文本框,该文本框不得超过200个字符.

谁能上前

Good evening to everyone............
I just need a code to validate a textbox that should not allow more than 200 characters.

Can anyone step forward

推荐答案

你好,

将其MaxLenght设置为200
Hello,

Set it''s MaxLenght to 200


处理TectChanged事件,然后执行以下操作:

Handle the TectChanged event, and do something like this:

TextBox ctrl = sender as TextBox;
if (ctrl.Length > 200)
{
    ctrl.Text = ctrl.Text.Substring(0,200);
}
labelCtrl.Text = string.Format("{0} of 200 characters", ctrl.Text.Length);


另一种解决方案是使用Control中的.Validating事件.然后,您可以使用ErrorProvider-Control向用户显示他在做什么错.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx [ ^ ]
Another solution would be to use the .Validating event from the Control. You can than use a ErrorProvider-Control to show the user what he is doing wrong.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx[^]


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

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