Reputation: 91
i need to hide the submit but if there is any error in validation. i'm using the below code where if i entered both the text boxes with characters and if i correct 1 of the text box the submit button turns visible! how to avoid it util all the errors are clear? Thank you
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, "");
}
}
Upvotes: 2
Views: 638
Reputation: 19863
Depending on the number of textboxes you validate, you could create a function that validates everything in one shot.
bool ValidateAll(){
bool isNum = int.TryParse(textBox5.Text.Trim(), out num);
if (!isNum)
{
return false;
}
isNum = int.TryParse(textBox6.Text.Trim(), out num);
if (!isNum)
{
return false;
}
return true;
}
Then call this method of all TextChanged event you want to monitor
Upvotes: 0
Reputation: 6075
Check that both text boxes are error free before setting the button visibility to True
. You can use another method for this, as I did below using UpdateSubmitButton
.
This method checks if either textBox5
or textBox6
has an error associated with it, then updates the visibility of button2
accordingly. Note that I removed the other button2.Visible
assignments from each of the TextChanged
events, and replaced it with a call to the UpdateSubmitButton
method.
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();
}
Upvotes: 1