Capturing "validating" event in TextBox of form negates normal "esc key" action

S

Steve Roggow

I am using a form that contains a TextBox class object of
which I capture the Validating event. This form is shown
as a dialog box. The problem is this: A user enters
invalid characters into the TextBox object and then
changes his/her mind and hits the escape key. Normally,
the ShowDialog function returns as if the user pressed
the cancel button - and this is the desired action.
However, with "validating" captured, the system invokes
my validating code BEFORE anything else (that I can
determine). There is no way to know that the user is
wanting to simply leave, so I need to pop up another
dialog box that complains about the invalid entry and
from here, the user may exit the form. I tried capturing
the events: "Leave, Keydown, Keypressed", etc to see if
these get invoked before "Validating" and no-dice.

The lack of any other postings regarding this , what I
would consider a common problem, tells me that I have
probably overlooked something obvious ...

Thanks for any help!
 
B

Brian

You can give this a try:

Dim check As Boolean = False

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If check Then
MessageBox.Show("Validating")
End If
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs)Handles
check = True
TextBox1_Validating(sender, New
System.ComponentModel.CancelEventArgs())
check = False
End Sub


Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Escape Then Close()
End Sub

Basically, you're calling the Validating event when the text changes. All
other calls will be rejected.
 
S

Steve

Brian,

Thanks for taking the time to reply.. Yes, your
suggestion may be a valid solution in VB (I am using C#)
and logically, your code makes sense. I have found in C#
that the escape key seems to bypass many of
the "expected" invocations such as the key down event. In
my case, the form with the text box in it simply returns
if as a person hit the cancel key. However, not before
the "validating" event is invoked! After posting this
morning, I continued working the problem and finally just
left the validating event out and used the "Leave" event
for the text box instead. I did my validating in the
Leave event handler. This causes the expected result of
exiting unconditionally when the user presses the escape
key, but this created a problem if the user clicked on
the cancel button - of course the "Leave" event handler
was called here also. I fixed this problem by checking
(within my "Leave event" handler) to see if the cancel
button had the focus and if it did, simply returned
without validating! Boy, some of the obfuscated things
that one has to do!

Thanks again for the reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top