2014-12-08 350 views
0

我在创建需要用户在字段中输入信息的窗体时遇到问题,请确认电子邮件和密码条目,然后在所有这些字段都为空时转到下一个窗体匹配/填写。单独的代码我有工作,但我似乎无法找到一种方法来满足所有需求,然后再进入下一个表单。目前它只是进入下一个表格,如果我点击继续按钮。在c#visual studio中验证必填字段并确认字段

一些代码,我的摘录:

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(); 
+0

这是Windows窗体? Web窗体? MVC? – 2014-12-08 05:00:35

+0

对于在问题中没有明确说明,我表示歉意。它在Windows窗体应用程序中。 – avgstudent 2014-12-08 05:04:51

+0

谷歌“窗体表单字段验证”,第一个命中是[Windows窗体中的用户输入验证](http://msdn.microsoft.com/en-us/library/ms229603%28v=vs.110%29.aspx ) – 2014-12-08 05:06:31

回答

0

被创建,如果结果无论打开,因为它的代码是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(); 
    } 
} 
0
if (! txtBoxEmail.Text.Equals(txtBoxConfirmEmail.Text)) 
{ 
    lblEmailError.Text = ("Email reentry does not match. Please reenter."); 
} 

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

您使用在Visual Studio 2012可以使用字段验证.aspx文件中为您希望的任何领域的Web应用程序的形式在表单提交之前验证。在C#中编写所有东西要容易得多。

+0

嗨,谢谢你的建议。虽然我希望我可以使用字段验证器,但我必须编写代码,因为我正在为项目执行该代码。这个特殊的例子需要一个循环语句吗?当我添加打开新表单的代码时,似乎忽略了条件语句。 – avgstudent 2014-12-08 04:49:41

+0

尝试删除this.hide并使用重定向(JSP样式)等。我确信ASP中也有重定向。 – Chetandalal 2014-12-08 04:54:25

0

试试这个:

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(); 
}