如何全局定义验证功能 [英] how to define validation function globally

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

问题描述

大家好,
抱歉,但是我是C#的新手....
任何朋友可以告诉我如何定义文本框的验证,例如仅数字,char.only等....实际上,我的问题是我想定义这些函数.我需要以其他2形式....

请任何哥们告诉我我该怎么办......?


C#窗口应用程序

hello guys,
sorry but i m new in C#....
can any buddy tell me how to define validation for text box like numbers only,char.only etc.... actually my problem is that i want to define these function.in such way ....i can call this function directly as i need in different-2 form ....

please can any buddy give me idea what i have to do......?


C# window application

推荐答案

讨论了许多Windows窗体验证技术^ ].
其中,使用MaskedTextBox控件的验证和事件驱动的验证是更常见的方法.

看看验证事件 [ ^ ].
最后,此处 [
A number of Windows Forms validations techniques are discussed here[^].
Amongst them, Validation with the MaskedTextBox Control and Event Driven validation are more common approaches.

Have a look at the validating event as well[^].
Finally, here[^] is a very simple example to get you started.


创建一个静态类,例如ReqFunctions.将方法设置为静态,然后可以从您正在使用的表单中调用它们.

例如在我的头顶上:
Create a static class such as ReqFunctions. Make the methods as static and then you can call them from the form that you are working.

for example on top of my head:
class ReqFunctions
{
  public static bool ValidateTextBox(string txtBoxValue, string validationType)
  {    
    if(validatioType == "Number")
    {
      Regex isnumber = new Regex("[^0-9]");
      return !isnumber.IsMatch(txtBoxValue);    
    }
    if(validationType == "Char")
    {
      Regex ischar = new Regex("^[a-zA-Z]+


"); 返回!ischar.IsMatch(txtBoxValue); } } }
"); return !ischar.IsMatch(txtBoxValue); } } }




在您的代码中,您可以将该函数称为




In your code you can call this function as

ReqFunctions.ValidateTextBox(txtTextbox1.Text);



您还可以检查
MSDN链接 [ ^ ].
最近,我也发现了这一点:仅数字和字符C#中的文本框验证! [



You can also check this MSDN link[^].
Recently I have found this also: Numbers and Characters only Textbox Validation in C# ![^]


这篇关于如何全局定义验证功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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