Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 验证多个表单,而不必在每个表单中都有验证代码_C#_Forms_Validation - Fatal编程技术网

C# 验证多个表单,而不必在每个表单中都有验证代码

C# 验证多个表单,而不必在每个表单中都有验证代码,c#,forms,validation,C#,Forms,Validation,我正在用C语言开发一个应用程序,它的表单接收来自不同州用户的相似输入 我需要能够验证它们,因此我使用errorproviders 代码运行良好,但我注意到,根据我目前的知识,如果我想验证多个表单,那么我必须在每个表单中复制粘贴类似表单的验证代码,我想知道是否有一种更简单的方法可以重用验证代码,将它放在所有表单都可以访问的中心位置,而不必为每个表单编写代码 下面是用C语言编写的验证代码片段 //Methods to verify and user inputs private bo

我正在用C语言开发一个应用程序,它的表单接收来自不同州用户的相似输入

我需要能够验证它们,因此我使用errorproviders

代码运行良好,但我注意到,根据我目前的知识,如果我想验证多个表单,那么我必须在每个表单中复制粘贴类似表单的验证代码,我想知道是否有一种更简单的方法可以重用验证代码,将它放在所有表单都可以访问的中心位置,而不必为每个表单编写代码

下面是用C语言编写的验证代码片段

//Methods to verify and user inputs
        private bool ValidateName()
        {
            bool bStatus = true;
            if (name.Text == string.Empty)
            {
                errorProvider2.SetError(name, "");
                errorProvider1.SetError(name, "Please Enter Name");
                bStatus = false;
            }
            else
            {
                errorProvider1.SetError(name, "");
                errorProvider2.SetError(name, "Good");
                bstatus = true;
            }
            return bStatus;
        }
        private void name_Validating(object sender, CancelEventArgs e)
        {
            ValidateName();
        }

我想做的是定义ValidateName方法,这样我就可以在表单的name\u Validating函数中调用它,该函数有一个名为name的文本框来验证它。

您将需要类似的东西。不在项目前面,所以没有精确的语法。但它应该为你指明正确的方向

//Methods to verify and user inputs
private bool ValidateName(string aName)
{
    bool bStatus = true;
    // You'll need to fill this bit
    // cast or instatiate a textbox here, let's call it txt_box_name
    // 
    // cast : if you pass an object that you know is a textbox 
    // 
    // instantiate : you can create an instance of a textbox with Activator.CreateInstance
    //   more on that here: http://msdn.microsoft.com/en-us/library/system.activator.createinstance%28v=vs.110%29.aspx
    //   
    //  and then continue as normal with your generic text box field
    if (txt_box_name.Text == string.Empty)
    {
        // do something
    }
    else
    {
        // do something else
    }
    return bStatus;
}
private void name_Validating(object sender, CancelEventArgs e)
{
    ValidateName("name");
    // or :
    //ValidateName("username");
}

我不是100%确定你想要做什么,但听起来你想要的基本上是用一些参数调用ValidateName,这样你就可以重用代码,假设唯一的区别是你传递的参数。是的,这就是我想要做的我想能够重用代码,但是我不知道怎么做有什么想法?有什么变化,怎么变化?文本框的名称可能会发生变化,比如在表格1中,我有一个文本框,它会得到用户名,在第二个表格中命名为name,我可以将它命名为username,但如果这是一个问题,我可以对任何文本框使用相同的名称,它通过应用程序接受用户名,我已经尝试过了,但问题是我应该把这个代码放在哪里?我把他们分在一个单独的班上吗?我在把问题贴到这里之前试过了,有错误…视情况而定。按钮单击的回调通常在代码隐藏中,因此您可以在那里执行。否则,您必须将其放置在codebehind可以访问的位置,以便启动该方法。