首页 文章

多个textBox验证及其组合

提问于
浏览
-3

我的WPF中有多个textBox,并想根据所需的应用程序逻辑检查它们是否正确填写:

textBox1 - 总是mandadory填写
textBox2 - 总是mandadory填写

textBox3 - if!string.IsNullOrEmpty - > textBox4也必须填写
textBox4 - if!string.IsNullOrEmpty - > textBox3也必须填写
textBox5 - if!string.IsNullOrEmpty - > textBox6也必须填写
textBox6 - if!string.IsNullOrEmpty - > textBox5也必须填写

所以texBox3和4都填写了-OR- textBox5和6或者全部3 4 5 6 .
我试过if语句,if语句带||布尔逻辑等 . 我总是陷入困境,最终得到一个不起作用的解决方案 .

这是一个直接的开始,但从来没有进一步的东西可行:

if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox1.Text))                    
  {
     MessageBox.Show("Fill out textBox1 and textBox2");                
  }

提前感谢您的任何建议 .

1 回答

  • 1
    if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox1.Text))                    
      {
         MessageBox.Show("Fill out textBox1 and textBox2"); 
         return;               
      }
    // two filled out or two empty
    if (string.IsNullOrEmpty(textBox3.Text) != string.IsNullOrEmpty(textBox4.Text)) 
    {
         MessageBox.Show("Fill out or empty textBox3 and textBox4");
         return;                
    }
    // two filled out or two empty
    if (string.IsNullOrEmpty(textBox5.Text) != string.IsNullOrEmpty(textBox6.Text)) 
    {
         MessageBox.Show("Fill out or empty textBox5 and textBox6"); 
         return;               
    }
    // if all empty error
        if (string.IsNullOrEmpty(textBox3.Text) && string.IsNullOrEmpty(textBox5.Text)) 
        {
             MessageBox.Show("Fill out 3,4 or 5,6 or 3,4,5,6");                
        }
    

相关问题