2

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, "");
    }
} 

2 Answers 2

1

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();
} 
Sign up to request clarification or add additional context in comments.

1 Comment

'System.Windows.Forms.TextBox' does not contain a definition for 'GetError' and no extension method 'GetError' accepting a first argument of type 'System.Windows.Forms.TextBox' could be found
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.