Validating textbox w/ Cancel button

D

Darin

I have a form w/ a textbox and Cancel button on it. I have a routine to
handle textbox.validating, and I have the form setup so the Cancel
button is the Cancel button.

WHen the user clicks on the cancel button, the textbox.validating is
being called. I don't want it to be since they are exiting the screen
the validation doesn't have to be done.

How can I do that.

Thanks.

Darin
 
G

Guest

if you want to exit the form when the user presses cancel then just say, in
the btnCancel click event

Me.Close()

and the form will close
 
D

Darin

I know that, but the problem is when the user clicks EXIT, the
textbox.validating event is called BEFORE the cancel.click is done, so
the textbox is being validated. It doesn't need to be validated because
the user is exiting.

Darin
 
G

Guest

Yes, closing the form causes 'validating' to take place. One, remove the
"controlBox" from the form, so the user cant close it that way. Then in the
btnCancel_click event, either set a module level variable like mCanceling to
True and then in the validating event ... If not mCanceling ...
Another approach. in the btnCancel_Click event:
Removehandeler TextBox1.validating, Addressof TextBox1_Validating
 
A

Al Reid

Terry said:
Yes, closing the form causes 'validating' to take place. One, remove the
"controlBox" from the form, so the user cant close it that way. Then in the
btnCancel_click event, either set a module level variable like mCanceling to
True and then in the validating event ... If not mCanceling ...
Another approach. in the btnCancel_Click event:
Removehandeler TextBox1.validating, Addressof TextBox1_Validating

That doesn't work either, at least not for VB2005. I've been fighting the same problem for some time now. When you click on the
button the validating event fires. If you set e.Cancel=True the button events never fire.

I think that the only way to do this is to grab the mouse coordinates in the validating event, do a WindowFromPoint API call, then
compare the returned HWND to the Cancel Button's Handle. If the handle matched then immediately exit the Validating event without
setting e.Cancel = True.
 
G

Guest

Well it works for me - but one thing I left out - you need to set the cancel
buttons 'causes validation' to false. The following code works as you might
hope:
Public Class Form1
Private mCanceling As Boolean = False

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
mCanceling = True
Me.Close()
End Sub

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If Not mCanceling Then
If TextBox1.Text.Length < 5 Then
e.Cancel = True
End If
End If
End Sub
End Class
The removehandeler method also works.

Also, as I just discovered, if you set the text box's causesvalidation to
false, you can close the form from the control box without validation taking
place. This really sounds more like a bug to me.
 
A

Al Reid

I had tried all of that, *BUT*, I had neglected to set the button's CausesValidation to False. DUH!!!
It does indeed work. I also got the WindowFromPoint approach working as well. Now I can go back and strip that out.

Thanks.
 
D

Darin

That doesn't work for me. I am on VB.Net, VS 2003. When I click on the
cancel button it doesn't even get to the cancel.click event until AFTER
it has done the validating.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MessageBox.Show("Button1_click")
mCanceling = True
Me.Close()
End Sub

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If Not mCanceling Then
If TextBox1.Text.Length < 5 Then
e.Cancel = True
End If
End If
End Sub

Run the program (make sure the tab stop has the textbox first then the
button), leave the text in textbox to be textbox, click on button, you
will see the message. Run it again, clear out the text in textbox, click
on cancel and you will not see the message - the system is not honoring
the cancel.click because (I guess) the e.cancel=true in _validating.


Darin
 
A

Al Reid

Just a follow-up. The thing that this approach does not allow (unless I've one again neglected the obvious) is using the escape key
to trigger the form cancel. If I hit the escape, the validation event fires and since the mCanceling is not True, the validation
error gets displayed before the form closes.

My final solution, unless someone has a better suggestion, is to change the Validating event to the Leave event and detecting
whether the cancel button had been clicked in the Leave event and exiting if it was. This way both the escape key and the cancel
button closes the form without causing the validation to occur.

The code to tell if the cancel button has been clicked is:

///////

Public Declare Function WindowFromPoint Lib "user32" (ByVal p As POINTAPI) As IntPtr

Public Structure POINTAPI

Dim X As Integer

Dim Y As Integer

End Structure



Private Function CancelButtonClicked() As Boolean

Dim p As New POINTAPI

p.X = Form.MousePosition.X

p.Y = Form.MousePosition.Y

Dim HWnd As IntPtr = WindowFromPoint(p)

If HWnd = CType(Me.CancelButton, Button).Handle Then

Return True

Else

Return False

End If

End Function

\\\\\\\\\\\\
 
G

Guest

Hi Al,
Don't know if you have been following the thread I started about the
escape key "bug", but if you search this community for "CancelButton vs
Escape Key", you can see the result including the work around for the escape
key. Also note that the "trick" to closing the form is not keeping a
seperate "canceling" flag, but to put "e.cancel=False" in the forms closing
event.
 
A

Al Reid

Terry,

Yes I've been following it. I added the code to handle the esc key per the "bug" thread and it works. However, after I removed the
"canceling" flag and added the "e.cancel=False" to the form's FormClosing event, I once again see the validation when clicking on
the cancel button, but the form then closes after displaying the validation error. I think it works better with the canceling flag.
 
G

Guest

Ok, I remember now, you are using a modal msgbox in your validation code. In
this case, you will need the mCanceling flag. If you were using an
errorprovider, then the error would be set, the focus would be returned to
the textbox, the form closing event would then fire and it changes the cancel
flag to false and the form closes. Its only because of the messagebox that
you need to keep the flag.
 

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