locked
Block Textbox Validation On Form Closing RRS feed

  • Question

  • Hai

    In my forms i dont want to arise the textbox validation when user close the form.Now when user is clicking  close button validation of previously activated textbox is arising.How can i stop that ..?  User can use normal close icon of windows form as well as the button given in the form for closing form.Please some one help me.

    Thanks


    • Edited by Avanthika Tuesday, June 3, 2014 8:21 AM
    Tuesday, June 3, 2014 8:13 AM

Answers

  • Hi, try below code to block textbox validation when user click the close icon of windows from(assume the name of textbox is textbox1):

    const int WM_CLOSE = 0x10;
    
    protected override void WndProc(ref Message m)
    {
        Console.WriteLine(m);
        if (m.Msg == WM_CLOSE)
        {
            textBox1.CausesValidation = false;
        }
        base.WndProc(ref m);
    }

    try below code to handle textbox.GotFocus and LostFocus event to block textbox validation when user click the button to close the form(assume the name of close button is btClose):

    private void textBox1_GotFocus(object sender, EventArgs e)
    {
        textBox1.CausesValidation = true;
    }
    
    private void textBox1_LostFocus(object sender, EventArgs e)
    {
        if (btClose.Focused)
        {
            textBox1.CausesValidation = false;
        }
    }

    • Marked as answer by Barry Wang Friday, June 13, 2014 9:30 AM
    Tuesday, June 3, 2014 10:26 AM
  • Hai

    In the form 13 textboxes is there and 4 text box is set as mandatory.In the form close how we know which was the textbox having focus. Instead  of

      textBox1.CausesValidation = false;

    what i want to do..?


    You could get a reference to the focused TextBox using the Form.ActiveControl property:

    if (m.Msg == WM_CLOSE)
    {
     Control control = this.ActiveControl;
     TextBox txtBox = control as TextBox;
    }
    

    • Marked as answer by Barry Wang Friday, June 13, 2014 9:30 AM
    Tuesday, June 3, 2014 10:58 AM

All replies

  • Hi, try below code to block textbox validation when user click the close icon of windows from(assume the name of textbox is textbox1):

    const int WM_CLOSE = 0x10;
    
    protected override void WndProc(ref Message m)
    {
        Console.WriteLine(m);
        if (m.Msg == WM_CLOSE)
        {
            textBox1.CausesValidation = false;
        }
        base.WndProc(ref m);
    }

    try below code to handle textbox.GotFocus and LostFocus event to block textbox validation when user click the button to close the form(assume the name of close button is btClose):

    private void textBox1_GotFocus(object sender, EventArgs e)
    {
        textBox1.CausesValidation = true;
    }
    
    private void textBox1_LostFocus(object sender, EventArgs e)
    {
        if (btClose.Focused)
        {
            textBox1.CausesValidation = false;
        }
    }

    • Marked as answer by Barry Wang Friday, June 13, 2014 9:30 AM
    Tuesday, June 3, 2014 10:26 AM
  • Hai

    In the form 13 textboxes is there and 4 text box is set as mandatory.In the form close how we know which was the textbox having focus. Instead  of

      textBox1.CausesValidation = false;

    what i want to do..?

    Tuesday, June 3, 2014 10:44 AM
  • Hai

    In the form 13 textboxes is there and 4 text box is set as mandatory.In the form close how we know which was the textbox having focus. Instead  of

      textBox1.CausesValidation = false;

    what i want to do..?


    You could get a reference to the focused TextBox using the Form.ActiveControl property:

    if (m.Msg == WM_CLOSE)
    {
     Control control = this.ActiveControl;
     TextBox txtBox = control as TextBox;
    }
    

    • Marked as answer by Barry Wang Friday, June 13, 2014 9:30 AM
    Tuesday, June 3, 2014 10:58 AM