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

  • 本文关键字:是否 文本 c# validation
  • 更新时间 :
  • 英文 :


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

private void buttonSave_Click(object sender, EventArgs e)
{
nameChosenOnCard  = textBoxName.Text;
numOfCard = textBoxCardNum.Text;
cardStartDate = textBoxStartMonth.Text + "/" + textBoxStartYear.Text;
cardEndDate = textBoxEndmonth.Text + "/" + textBoxEndYear.Text;
nameDetailsOnCard = textBoxNameOnCard.Text;
cardCVC= textBoxCVC.Text;
DialogResult = DialogResult.OK;
if (string.IsNullOrEmpty(textBoxName.Text) ||
string.IsNullOrEmpty(textBoxCardNum.Text) &&
string.IsNullOrEmpty(textBoxStartMonth.Text) ||
string.IsNullOrEmpty(textBoxStartYear.Text) ||
string.IsNullOrEmpty(textBoxEndmonth.Text) ||
string.IsNullOrEmpty(textBoxEndYear.Text) ||
string.IsNullOrEmpty(textBoxNameOnCard.Text) ||
string.IsNullOrEmpty(textBoxCVC.Text))
{
buttonSave.Enabled = false;
}
else
{
buttonSave.Enabled = true;
}
}

保存第一个

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

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

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

一种可能的解决方案是将所有文本框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;
}

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

相关内容

  • 没有找到相关文章