尝试检查文本框中是否输入了值

时间:2021-05-04 14:56:24

标签: c# validation

我试图找出我的代码有什么问题,我试图通过检查用户是否在文本框中输入了任何值来进行数据验证,但是当我运行代码时,它将继续保存卡,无论文本框中是否输入了值。

values=

3 个答案:

答案 0 :(得分:2)

先保存

 DialogResult = DialogResult.OK;

然后验证并尝试行动

if (...)
{
    buttonSave.Enabled = false;
}

让我们提取一个验证方法:

// Let's be more friendly and show to user which control has invalid value
private Control InvalidControl() {
  //TODO: Add more conditions here - if year is an integer in 1990..2100 range etc.
  if (string.IsNullOrEmpty(textBoxName.Text))
    return textBoxName;
  if (string.IsNullOrEmpty(textBoxCardNum.Text) && 
      string.IsNullOrEmpty(textBoxStartMonth.Text))
    return textBoxCardNum;  
  if (string.IsNullOrEmpty(textBoxStartYear.Text))
    return textBoxStartYear; 
  if (string.IsNullOrEmpty(textBoxEndmonth.Text))
    return textBoxEndmonth; 
  if (string.IsNullOrEmpty(textBoxEndYear.Text))
    return textBoxEndYear; 
  if (string.IsNullOrEmpty(textBoxNameOnCard.Text))
    return textBoxNameOnCard; 
  if (string.IsNullOrEmpty(textBoxCVC.Text))
    return textBoxCVC;

  return null;
}

然后当且仅当所有控件都有效时我们才能保存:

private void buttonSave_Click(object sender, EventArgs e) {
  Control failed = InvalidControl();

  if (failed != null) {  // we have an invalid control
    if (failed.CanFocus) // let user know which is it:
      failed.Focus();    // we can set keyboard focus on it

    // Let user have a message on what's going on
    MessageBox.Show("Invalid value", //TODO: put a better text here 
                     Application.ProductName, 
                     MessageBoxButtons.OK, 
                     MessageBoxIcon.Warning);
  }
  else // Validation is OK, we can close the dialog with "OK" result 
    DialogResult = DialogResult.OK; 
}

如果您想启用/禁用 buttonSave,您可以为感兴趣的所有控件实现 TextChanged 事件处理程序: textBoxNametextBoxCardNum、...、textBoxCVC 等等

private void Textboxes_TextChanged(object sender, EventArgs e) {
  // we can save if and only if we have no invalid control(s)
  buttonSave.Enabled = InvalidControl() == null; 
}

答案 1 :(得分:0)

在检查所有文本框时,这比使用杂乱的比较运算符更有效

List<TextBox> txts = panelName.Controls.OfType<TextBox>(); //just change the 'panelName' whether all your textboxes are located inside a form or just a panel.
foreach(TextBox txt in txts)
{
    if(string.IsNullOrEmpty(txt.Text)
        buttonSave.Enabled = false;
    else
        buttonSave.Enabled = true;
}

答案 2 :(得分:0)

一种可能的解决方案是将所有文本框 TextChanged 事件连接(订阅)到同一事件。然后,当其中一个文本框中的文本发生变化时,该事件将触发。在这种情况下,您可以检查每个文本框以查看它们是否都有一些文本,如果有,则启用保存按钮,否则禁用保存按钮。

例如,将四 (4) 个文本框连接到同一个 TextChanged 事件可能看起来像……

textBox1.TextChanged += new EventHandler(textBox_TextChanged);
textBox2.TextChanged += new EventHandler(textBox_TextChanged);
textBox3.TextChanged += new EventHandler(textBox_TextChanged);
textBox4.TextChanged += new EventHandler(textBox_TextChanged);

为了提供帮助,List<TextBox> 可能会派上用场来循环我们想要的文本框。当表单加载时,代码可以将文本框添加到此列表中,例如……

TextBoxes = new List<TextBox>();
TextBoxes.Add(textBox1);
TextBoxes.Add(textBox2);
TextBoxes.Add(textBox3);
TextBoxes.Add(textBox4);

最后,在 tetxBox_TextChanged 事件中进行所有比较。这可能看起来像……

List<TextBox> TextBoxes;

private void Form2_Load(object sender, EventArgs e) {
  btnSave.Enabled = false;
  textBox1.TextChanged += new EventHandler(textBox_TextChanged);
  textBox2.TextChanged += new EventHandler(textBox_TextChanged);
  textBox3.TextChanged += new EventHandler(textBox_TextChanged);
  textBox4.TextChanged += new EventHandler(textBox_TextChanged);

  TextBoxes = new List<TextBox>();
  TextBoxes.Add(textBox1);
  TextBoxes.Add(textBox2);
  TextBoxes.Add(textBox3);
  TextBoxes.Add(textBox4);
}


private void textBox_TextChanged(object sender, EventArgs e) {
  foreach (TextBox tb in TextBoxes) {
    if (string.IsNullOrEmpty(tb.Text)) {
      btnSave.Enabled = false;
      return;
    }
  }
  btnSave.Enabled = true;
}

使用这种方法,当所有文本框都有一些文本时,保存按钮将被启用,并且在任何文本框为空时将被禁用。