gpt4 book ai didi

c# - 如果验证中有任何错误,请禁用提交按钮

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:55 25 4
gpt4 key购买 nike

我需要隐藏提交,但如果验证有任何错误。我正在使用下面的代码,如果我输入了两个带有字符的文本框,并且如果我更正了其中的一个文本框,则提交按钮将变为可见!如何在所有错误都清楚的情况下避免它?谢谢

int num;

private void textBox5_TextChanged(object sender, EventArgs e)
{
bool isNum = int.TryParse(textBox5.Text.Trim(), out num);
if (!isNum)
{
button2.Visible = false;
errorProvider1.SetError(this.textBox5, "Please enter numbers");
}
else
{
button2.Visible = true;
errorProvider1.SetError(this.textBox5, "");
}
}

private void textBox6_TextChanged(object sender, EventArgs e)
{
bool isNum = int.TryParse(textBox6.Text.Trim(), out num);
if (!isNum)
{
button2.Visible = false;
errorProvider2.SetError(this.textBox6, "Please enter numbers");
}
else
{
button2.Visible = true;
errorProvider2.SetError(this.textBox6, "");
}
}

最佳答案

在将按钮可见性设置为 True 之前,检查两个文本框是否没有错误。您可以为此使用另一种方法,就像我在下面使用 UpdateSubmitButton 所做的那样。

此方法检查 textBox5textBox6 是否有与之关联的错误,然后相应地更新 button2 的可见性。请注意,我从每个 TextChanged 事件中删除了其他 button2.Visible 分配,并将其替换为对 UpdateSubmitButton 方法的调用。

private void UpdateSubmitButton()
{
if (String.IsNullOrEmpty(errorProvider1.GetError) &&
String.IsNullOrEmpty(errorProvider2.GetError))
{
button2.Visible = true;
}
else
{
button2.Visible = false;
}
}

private void textBox5_TextChanged(object sender, EventArgs e)
{
int num;
bool isNum = int.TryParse(textBox5.Text.Trim(), out num);
if (!isNum)
{
errorProvider1.SetError(this.textBox5, "Please enter numbers");
}
else
{
errorProvider1.SetError(this.textBox5, "");
}
UpdateSubmitButton();
}

private void textBox6_TextChanged(object sender, EventArgs e)
{
int num;
bool isNum = int.TryParse(textBox6.Text.Trim(), out num);
if (!isNum)
{
errorProvider2.SetError(this.textBox6, "Please enter numbers");
}
else
{
errorProvider2.SetError(this.textBox6, "");
}
UpdateSubmitButton();
}

关于c# - 如果验证中有任何错误,请禁用提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com