Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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#visual studio中验证必填字段并确认字段_C#_Winforms - Fatal编程技术网

在c#visual studio中验证必填字段并确认字段

在c#visual studio中验证必填字段并确认字段,c#,winforms,C#,Winforms,我在创建表单时遇到问题,该表单要求用户在字段中输入信息,确认电子邮件和密码输入,并在匹配/填写所有字段后进入下一个表单。就个人而言,我拥有的代码是有效的,但我似乎无法找到一种方法来实现它,以便在进入下一个表单之前满足所有要求。现在,如果我单击continue按钮,它将进入下一个表单 我收到的一些代码摘录如下: if (string.IsNullOrEmpty(email)) { lblRequirementsError.Text = ("All required fields have

我在创建表单时遇到问题,该表单要求用户在字段中输入信息,确认电子邮件和密码输入,并在匹配/填写所有字段后进入下一个表单。就个人而言,我拥有的代码是有效的,但我似乎无法找到一种方法来实现它,以便在进入下一个表单之前满足所有要求。现在,如果我单击continue按钮,它将进入下一个表单

我收到的一些代码摘录如下:

if (string.IsNullOrEmpty(email))
{
    lblRequirementsError.Text = ("All required fields have not been filled.");
}

if (txtBoxEmail.Text != txtBoxConfirmEmail.Text)
{
    lblEmailError.Text = ("Email reentry does not match. Please reenter.");
}

if (txtBoxPassword.Text != txtBoxConfirmPassword.Text)
{
    lblPasswordError.Text = ("Password reentry does not match. Please reenter.");
}

this.Hide();
frmBilling secondForm = new frmBilling();
secondForm.Show();

您是否在visual studio 2012中使用web应用程序表单。您可以在.ASPX文件中使用字段验证器,用于提交表单之前要验证的任何字段。这比用C#编写所有内容要容易得多。

如果要将控件绑定或转换为数据对象,则可以使用DataAnnotation。这样就很容易验证了。请查看链接以了解更多详细信息


问题在于无论if结果如何,表单都会被创建和打开,因为表单的代码在ifs之外。首先,检查所有字段是否为空,然后检查是否满足验证要求,然后打开新窗口。像这样的方法应该会奏效:

//If both email and password are not empty
if (!string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(password))
{
    //if both email and password math the re entry
    if (txtBoxEmail.Text == txtBoxConfirmEmail.Text &&
        txtBoxPassword.Text == txtBoxConfirmPassword.Text)
    {
        //execute the code to open the new form
        this.Hide();
        frmBilling secondForm = new frmBilling();
        secondForm.Show();
    }
}
试试这个:

bool validationStatus = default(bool);

if (string.IsNullOrEmpty(email))
{
    lblRequirementsError.Text = ("All required fields have not been filled.");
    validationStatus  = true;
}

if (txtBoxEmail.Text != txtBoxConfirmEmail.Text)
{
    lblEmailError.Text = ("Email reentry does not match. Please reenter.");
    validationStatus  = true;
}

if (txtBoxPassword.Text != txtBoxConfirmPassword.Text)
{
    lblPasswordError.Text = ("Password reentry does not match. Please reenter.");
    validationStatus  = true;
}

if(!validationStatus)
{
   Hide();
   frmBilling secondForm = new frmBilling();
   secondForm.Show();
}

嗨,谢谢你的建议。虽然我确实希望可以使用字段验证器,但我必须在为项目编写代码时编写代码。此特定示例是否需要循环语句?当我添加用于打开新表单的代码时,它似乎忽略了条件语句。请尝试删除它。隐藏并使用类似重定向(JSP样式)的内容。我确信ASP中也有重定向。这是Windows窗体吗?网络表单?MVC?我很抱歉在问题中没有说清楚。它是在一个Windows窗体应用程序中。谷歌“Windows窗体字段验证”,第一个点击是
bool validationStatus = default(bool);

if (string.IsNullOrEmpty(email))
{
    lblRequirementsError.Text = ("All required fields have not been filled.");
    validationStatus  = true;
}

if (txtBoxEmail.Text != txtBoxConfirmEmail.Text)
{
    lblEmailError.Text = ("Email reentry does not match. Please reenter.");
    validationStatus  = true;
}

if (txtBoxPassword.Text != txtBoxConfirmPassword.Text)
{
    lblPasswordError.Text = ("Password reentry does not match. Please reenter.");
    validationStatus  = true;
}

if(!validationStatus)
{
   Hide();
   frmBilling secondForm = new frmBilling();
   secondForm.Show();
}