0

I have textbox validation for not empty and wrong value for edit mode, when i entered wrong data in textbox,i cannot click cancel button, Validation occurs.

this is for windows form application, i did validation.

if (dataLogic.IsEmpty(txtSubCourseName.Text))
            {
                dataLogic.DisplayEmptyMsg();
                e.Cancel = true;
                return;
            }
            if (tag != "New")
            {
                DataTable dt = new DataTable();
                sda = new SqlDataAdapter("SELECT * FROM tblSubCourse WHERE SubCourseName='" + txtSubCourseName.Text + "'", con);
                con.Open();
                sda.Fill(dt);
                con.Close();
                if (dt.Rows.Count > 0)
                {
                    txtSubCourseName.Text = dt.Rows[0]["SubCourseName"] + "";
                }
                else
                {
                    dataLogic.EmptyMsg();
                    e.Cancel = true;
                    return;
                }
            }

i expect to not validate when i click cancel button and escape button, even entered data is wrong.

2 Answers2

0

The reason why you do not have the Cancel click event fire is because your textbox had focus when you pressed on Cancel button and the Cancel button had its CausesValidation property set to true. This causes your textBox Validating event to fire since Cancel button is set to cause validation.

So, just set the CausesValidation= false for the Cancel and other buttons for which you want to skip the textBox Validating event.

 btnCancel.CausesValidation = false;

Also, make sure that when closing the form, no validation happens ( i.e. when clicking on close button of form or calling this.Close() method). Put the code below in form's load event.

this.Form1.AutoValidate = false;
Sunil
  • 20,653
  • 28
  • 112
  • 197
  • this does not Work – Amod Kumar Shah Jan 21 '19 at 09:58
  • Do you have a textbox validating event and a cancel button click event? – Sunil Jan 21 '19 at 10:09
  • Make sure to not call any validation logic in any of your buttons click event. Instead, put the validation code in validating event of text box. – Sunil Jan 21 '19 at 10:12
  • I added another piece of code. Please check that out since you may be closing your form from within Cancel button click and it will not close as long as the new property that I have just added is true. – Sunil Jan 21 '19 at 10:31
0

My solution was to simply declare the control at the beginning of the class and use it in each method.

public partial class Form1 : Form
{
    ErrorProvider errorProvider = new ErrorProvider();
    ...

    private void textBoxName_Validating(object sender, CancelEventArgs e)
    {
        if (string.IsNullOrWhiteSpace(textBoxName.Text))
        {
            e.Cancel = true;
            textBoxName.Focus();
            errorProvider.SetError(textBoxName, "Name should not be left 
            blank!");
        }
        else
        {
            e.Cancel = false;
            errorProvider.SetError(textBoxName, "");
        }
    }
Libni
  • 1