C# Text box validation on form close 
Author Message
 C# Text box validation on form close

Hi,

I have a textbox that has a CancelEventHandler attatched to it to
validate the data entered by the user. The problem is that if the
cursor is in the textbox when the user try's to close the window the
validation is fired and if the contents of the textbox are invalid the
window won't close.

How do I prevent this? Would I need to test within the validation
event handler for each possible event that could have happened that I
don't want the validation to happen for. If so how do you test for the
form close event?

Or is there an easier way?

Thanks in advance,
Si.



Sun, 22 May 2005 21:58:21 GMT  
 C# Text box validation on form close
Simon,

    Have you tried to set the CausesValidation property of the form to false
(if it isn't already).  If this is the case, then it should not cause the
textbox to throw the validate event.

    Hope this helps.

--
               - Nicholas Paldino [.NET/C# MVP]


Quote:
> Hi,

> I have a textbox that has a CancelEventHandler attatched to it to
> validate the data entered by the user. The problem is that if the
> cursor is in the textbox when the user try's to close the window the
> validation is fired and if the contents of the textbox are invalid the
> window won't close.

> How do I prevent this? Would I need to test within the validation
> event handler for each possible event that could have happened that I
> don't want the validation to happen for. If so how do you test for the
> form close event?

> Or is there an easier way?

> Thanks in advance,
> Si.



Sun, 22 May 2005 22:05:36 GMT  
 C# Text box validation on form close
Set the CausesValidation property of the close button to False. You might
also have to set the same property on the Form to false too, to stop the
window close button causing validation.

--
Nick Holmes
Coyote Software, GmbH.


Quote:
> Hi,

> I have a textbox that has a CancelEventHandler attatched to it to
> validate the data entered by the user. The problem is that if the
> cursor is in the textbox when the user try's to close the window the
> validation is fired and if the contents of the textbox are invalid the
> window won't close.

> How do I prevent this? Would I need to test within the validation
> event handler for each possible event that could have happened that I
> don't want the validation to happen for. If so how do you test for the
> form close event?

> Or is there an easier way?

> Thanks in advance,
> Si.



Sun, 22 May 2005 22:10:29 GMT  
 C# Text box validation on form close
This is a known bug.

There is a work around but it is pretty ugly.

    protected override void WndProc(ref Message m) {

            if (m.Msg == WM_CLOSE ||

                m.Msg == WM_QUERYENDSESSION ||

                m.Msg == WM_ENDSESSION) {

                textBox1.Validating -= new

System.ComponentModel.CancelEventHandler
(this.textBox1_Validating);

                base.WndProc(ref m);

                textBox1.Validating += new

System.ComponentModel.CancelEventHandler
(this.textBox1_Validating);

            } else {

                base.WndProc(ref m);

            }

        }

Quote:
>-----Original Message-----
>Hi,

>I have a textbox that has a CancelEventHandler attatched
to it to
>validate the data entered by the user. The problem is
that if the
>cursor is in the textbox when the user try's to close the
window the
>validation is fired and if the contents of the textbox
are invalid the
>window won't close.

>How do I prevent this? Would I need to test within the
validation
>event handler for each possible event that could have
happened that I
>don't want the validation to happen for. If so how do you
test for the
>form close event?

>Or is there an easier way?

>Thanks in advance,
>Si.
>.



Tue, 24 May 2005 03:35:09 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. C# ASP.NET Forms - Client Side Validation using Custom Validator Control

2. Microsoft Forms 2.0 Text Box

3. Wrapping text in text box or edit box

4. Access Validation error when closing application

5. How to Clear a Web Form That Has Validation

6. VB form -> C# form

7. modeless form using form.Show() method in C#

8. PBM: Message box not displayed after main dialog box is closed in MFC dialog-based app

9. include another form(cs) in C#?

10. NT account validation in C#

11. Date Validation in C#?

12. Text Field Validation

 

 
Powered by phpBB® Forum Software