473,695 Members | 2,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validating textbox w/ Cancel button

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

WHen the user clicks on the cancel button, the textbox.validat ing 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

*** Sent via Developersdex http://www.developersdex.com ***
Jul 12 '06 #1
21 9163
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
--
-iwdu15
Jul 12 '06 #2
I know that, but the problem is when the user clicks EXIT, the
textbox.validat ing 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

*** Sent via Developersdex http://www.developersdex.com ***
Jul 12 '06 #3
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.valida ting, Addressof TextBox1_Valida ting
--
Terry
"Darin" wrote:
I know that, but the problem is when the user clicks EXIT, the
textbox.validat ing 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

*** Sent via Developersdex http://www.developersdex.com ***
Jul 13 '06 #4
"Terry" <Te***@nospam.n ospamwrote in message news:90******** *************** ***********@mic rosoft.com...
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.valida ting, Addressof TextBox1_Valida ting
--
Terry

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.

--
Al Reid
Jul 13 '06 #5
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
mCanceling = True
Me.Close()
End Sub

Private Sub TextBox1_Valida ting(ByVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting
If Not mCanceling Then
If TextBox1.Text.L ength < 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 causesvalidatio n to
false, you can close the form from the control box without validation taking
place. This really sounds more like a bug to me.

--
Terry
"Al Reid" wrote:
"Terry" <Te***@nospam.n ospamwrote in message news:90******** *************** ***********@mic rosoft.com...
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.valida ting, Addressof TextBox1_Valida ting
--
Terry

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.

--
Al Reid
Jul 13 '06 #6
I had tried all of that, *BUT*, I had neglected to set the button's CausesValidatio n 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.

--
Al Reid (who didn't mean to hijack the thread)
"Terry" <Te***@nospam.n ospamwrote in message news:70******** *************** ***********@mic rosoft.com...
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
mCanceling = True
Me.Close()
End Sub

Private Sub TextBox1_Valida ting(ByVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting
If Not mCanceling Then
If TextBox1.Text.L ength < 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 causesvalidatio n to
false, you can close the form from the control box without validation taking
place. This really sounds more like a bug to me.

--
Terry


Jul 13 '06 #7
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
MessageBox.Show ("Button1_click ")
mCanceling = True
Me.Close()
End Sub

Private Sub TextBox1_Valida ting(ByVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting
If Not mCanceling Then
If TextBox1.Text.L ength < 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

*** Sent via Developersdex http://www.developersdex.com ***
Jul 13 '06 #8
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 CancelButtonCli cked() As Boolean

Dim p As New POINTAPI

p.X = Form.MousePosit ion.X

p.Y = Form.MousePosit ion.Y

Dim HWnd As IntPtr = WindowFromPoint (p)

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

Return True

Else

Return False

End If

End Function

\\\\\\\\\\\\

--
Al Reid

"Al Reid" <ar*****@reidDA SHhome.comwrote in message news:uA******** ******@TK2MSFTN GP05.phx.gbl...
I had tried all of that, *BUT*, I had neglected to set the button's CausesValidatio n 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.

--
Al Reid (who didn't mean to hijack the thread)
"Terry" <Te***@nospam.n ospamwrote in message news:70******** *************** ***********@mic rosoft.com...
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
mCanceling = True
Me.Close()
End Sub

Private Sub TextBox1_Valida ting(ByVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting
If Not mCanceling Then
If TextBox1.Text.L ength < 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 causesvalidatio n to
false, you can close the form from the control box without validation taking
place. This really sounds more like a bug to me.

--
Terry


Jul 13 '06 #9
Hi Al,
Hmmm, I just tried the 'escape' key and it works fine! The form cancels
and the 'validating' event is skipped. Sounds like you have something else
going on.
--
Terry
"Al Reid" wrote:
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 CancelButtonCli cked() As Boolean

Dim p As New POINTAPI

p.X = Form.MousePosit ion.X

p.Y = Form.MousePosit ion.Y

Dim HWnd As IntPtr = WindowFromPoint (p)

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

Return True

Else

Return False

End If

End Function

\\\\\\\\\\\\

--
Al Reid

"Al Reid" <ar*****@reidDA SHhome.comwrote in message news:uA******** ******@TK2MSFTN GP05.phx.gbl...
I had tried all of that, *BUT*, I had neglected to set the button's CausesValidatio n 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.

--
Al Reid (who didn't mean to hijack the thread)
"Terry" <Te***@nospam.n ospamwrote in message news:70******** *************** ***********@mic rosoft.com...
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
mCanceling = True
Me.Close()
End Sub
>
Private Sub TextBox1_Valida ting(ByVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting
If Not mCanceling Then
If TextBox1.Text.L ength < 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 causesvalidatio n to
false, you can close the form from the control box without validation taking
place. This really sounds more like a bug to me.
>
--
>
>
Terry
>
>


Jul 13 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1369
by: Mike | last post by:
Hi, In one of my winforms I am validating a textbox making sure it was filled out and that the data is numeric and setting e.Cancel = true if not. Everything works fine however the form has a OK and Cancel button. I do not want the user be able to submit the form if the data is not correct (which works fine by setting e.Cancel = true) however I want to the user to be able to hit cancel and which closes the form. Right now by setting...
0
931
by: Bevo | last post by:
I have a textbox TB on my form. In TB's Validating-event I check on the input, if it's not ok I want to put the cursor back to the textbox and cancel further actions (that is, I assign e.Cancel = true). This seems to work fine when I just tab out of the text box or click on another textbox. But when I click on, say, a toolbar button the cancelling does not work. The toolbar button's Click-event's underlying code is executing anyhow. ...
4
3006
by: Eric | last post by:
Is there a way to cancel the validating event on the closing event? I have 2 textboxes that I use the validating event to check for numeric data. If I try to close the form without putting a number in, it calls the validate event and pops up my MsgBox. Thanks!
2
2124
by: Chris Dunaway | last post by:
I have a form with a textbox and numerous panels, buttons and other controls. I have handled the textbox Validating and Validated events. The textbox will hold a filename. In the validating event, I check that the string in the textbox is a file that exists or whether or not the string is blank and display a message box in either case. I also call e.Cancel so that the value will be corrected. However, certain buttons on the form...
0
2462
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls which are not behaving as I would expect. Specifically, if there is a command button external to the usercontrol which is activated by a shortcut key (eg Alt-B), the command button Click event handler code 'executes' even though the textbox set...
0
2435
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852 (http://support.microsoft.com/kb/810852), but then I realized that the hotfix mentioned was in .Net v1.1, which I am using. I took the sample from that article and recreated the situation I see in my application. (Code included below.) If you run the...
16
5502
by: Al Santino | last post by:
Hi, It appears displaying a messagebox in a validating event will cancel the subsequent event. In the program below, button 2's click event doesn't fire if you open a dialog box in button 1's validating event. Am I doing something wrong here? Thanks Al Imports system
1
3790
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I've noticed that controls do not raise a Validating event if they are contained in a ToolStripDropDown via a ToolStripControlHost item. Please run the following sample and follow the instructions on the form to reproduce this issue: ------------------------------------ Public Class Form1 Inherits Windows.Forms.Form
8
4125
by: Peted | last post by:
I have an amazing problem which i think i have no hope of solving Im working with a c# dot net module that is hosted by and runs under a delphi form envrioment. Dont ask me how this insanity has prevailed it just is :) My problem is this im trying to validate the contents of a textbox (it has to be a normal textbox) and on a c# winforms and i am calling the textbox validating
0
9112
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8826
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8818
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6487
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5832
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2996
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2261
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.