473,388 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Bug or Feature? CancelButton vs Escape Key

I have noiticed a change in behavior between VB6 and VB.Net (2003 and 2005)
that I don't find documented anywhere. It has to do with 'causesvalidation'
and the button on the Form defined to be the cancelbutton. According to the
documentation: "The cancel button for a form is the button control that is
clicked whenever the user presses the ESC key." Yet 'clicking' the button
and pressing the escape key do not produce the same results. Simple example:
One textbox, 1 button, the button is the forms 'cancelbutton', the buttons
'causesvalidation' property is False. The following code:
Public Class Form1

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

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = False
End Sub

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

Clicking the button works, tabing to the button and pressing the spacebar
works, closing the form from the controlbox works .... pressing the escape
button does NOT work! Surprisingly, if I change the TextBox causesvalidation
to False, then the escape key works! This sounds (looks) like a bug to me.
--
Terry
Jul 16 '06 #1
8 9957
Hi Terry,

Thanks for your post!

Based on my understanding, there are a TextBox and a Button on the form,
the Button is set to the CancelButton of Form. TextBox.CancelValidation
property is set to true, while Button.CancelValidation is set to false.
Button.Click event will invoke Me.Close method to close the Form. You find
that pressing ESC key does not close the Form. While after setting
TextBox.CancelValidation property to false, you can use ESC key to close
the Form now. If I have misunderstood you, please feel free to tell me,
thanks.

Yes, I can reproduce this behavior with the above conditions. Actually,
this behavior can only be reproduced when the initial focus is in TextBox,
not Button. If the Button control has a lower TabIndex property value, it
will has the initial focus when Form lauches, then pressing ESC will work
without any problem.

Actually, the logic is that the validating only applies to the
focused/active control on the Form. When the focus is in TextBox and we
press ESC, the CancelButton logic will invoke Button.PerformClick method.
Then the validation code in Button.PerformClick method will check that the
active Control(TextBox in our scenario)'s CancelValidation is true, so it
will run your TextBox1_Validating code, which fails to
validate(TextBox1.Text.Length < 5) and the ESC
operation(Button.PerformClick) is cancelled.

If the focus is on the Button, the Validating code will only check
Button.CancelValidation property, not TextBox.CancelValidation property. So
the ESC operation is not cancelled. So this is not a bug, but by design
behavior.

If you are curious, below is the logic code in Button.PerformClick method:

public void PerformClick()
{
if (base.CanSelect)
{
bool flag1;
bool flag2 = base.ValidateActiveControl(out flag1);
if (!base.ValidationCancelled && (flag2 || flag1))
{
base.ResetFlagsandPaint();
this.OnClick(EventArgs.Empty);
}
}
}
As you can see, Button.PerformClick method invokes ValidateActiveControl()
method to do the validation work.

So in your scenario, the key point to workaround the validation is setting
TextBox.CancelValidation to false, not Button.CancelValidation property.

Hope my analysis makes the problem clear. If you still have anything
unclear, please feel free to tell me, thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 17 '06 #2
Hi Jeffrey,
Thank you for your response. If I understand you correectly, you are
telling me that this undocumented change from VB6 is by design. That someone
decided that pressing the escape key should behave differently then clicking
the cancel button. Well then, imho that is a design bug! If clicking the
button with the mouse first makes it the active control followed my the click
event then the escape key should also make it the active control before
calling the PerformClick method. The point is, there really should be no
difference between clicking the button and pressing the escape key. The fix
of making the causesvalidation property for the textbox false implies that I
would have to do that for all the textbox controls on a form thereby
eliminating the ability to validate while moving between them. Of course, I
could go back to the way we did this before there was a validating event and
use the lostfocus event together with the focus() method to control it myself
- but wasn't the validating event put there to eliminate the need to do that?
Looks to me like the only true workaround for this is to not define a
cancelbutton and force the user to click it with the mouse.
--
Terry
""Jeffrey Tan[MSFT]"" wrote:
Hi Terry,

Thanks for your post!

Based on my understanding, there are a TextBox and a Button on the form,
the Button is set to the CancelButton of Form. TextBox.CancelValidation
property is set to true, while Button.CancelValidation is set to false.
Button.Click event will invoke Me.Close method to close the Form. You find
that pressing ESC key does not close the Form. While after setting
TextBox.CancelValidation property to false, you can use ESC key to close
the Form now. If I have misunderstood you, please feel free to tell me,
thanks.

Yes, I can reproduce this behavior with the above conditions. Actually,
this behavior can only be reproduced when the initial focus is in TextBox,
not Button. If the Button control has a lower TabIndex property value, it
will has the initial focus when Form lauches, then pressing ESC will work
without any problem.

Actually, the logic is that the validating only applies to the
focused/active control on the Form. When the focus is in TextBox and we
press ESC, the CancelButton logic will invoke Button.PerformClick method.
Then the validation code in Button.PerformClick method will check that the
active Control(TextBox in our scenario)'s CancelValidation is true, so it
will run your TextBox1_Validating code, which fails to
validate(TextBox1.Text.Length < 5) and the ESC
operation(Button.PerformClick) is cancelled.

If the focus is on the Button, the Validating code will only check
Button.CancelValidation property, not TextBox.CancelValidation property. So
the ESC operation is not cancelled. So this is not a bug, but by design
behavior.

If you are curious, below is the logic code in Button.PerformClick method:

public void PerformClick()
{
if (base.CanSelect)
{
bool flag1;
bool flag2 = base.ValidateActiveControl(out flag1);
if (!base.ValidationCancelled && (flag2 || flag1))
{
base.ResetFlagsandPaint();
this.OnClick(EventArgs.Empty);
}
}
}
As you can see, Button.PerformClick method invokes ValidateActiveControl()
method to do the validation work.

So in your scenario, the key point to workaround the validation is setting
TextBox.CancelValidation to false, not Button.CancelValidation property.

Hope my analysis makes the problem clear. If you still have anything
unclear, please feel free to tell me, thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 17 '06 #3
Hi Terry,

Thanks for your feedback!

Oh, yes, after giving your reply a second read, I think I understand you
much better.

Yes, it seems clicking on the button will not trigger the Validating event
and will close the Form because the Button.CauseValidation is set to false,
while pressing ESC does not close the Form because it triggers the
Validating event. This is really a strange behavior, the ESC key should
have the same behavior as Button.Click.

I will spend some more time in this issue to find out the root cause. I
will get back to you ASAP. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 18 '06 #4
Hi Terry,

Sorry for let you wait.

Yes, after performing some research, I found that some customers had
reported the similar issue as a bug. The problem lies in that the ESC key
causes the validating from Button.PerformClick method, while clicking
button will trigger the validating through Control.WmSetFocus internal
method. So it seems that Button.PerformClick method does not check
Button.CauseValidation correctly internally.

Please refer to the bug link below for more information:
"CausesValidation has no effect on Button when PerformClick is called. "
http://connect.microsoft.com/VisualS...k.aspx?Feedbac
kID=116242

To workaround this problem, you may intercept the ESC key explicitly and
set AutoValidate property to AutoValidate.Disable to disable the
validating, sample code snippet is listed below:

Protected Overrides Function ProcessDialogKey(ByVal keyData As
System.Windows.Forms.Keys) As Boolean
Dim cancel As Button = CType(Me.CancelButton, Button)
If keyData = Keys.Escape Then
Me.AutoValidate = Windows.Forms.AutoValidate.Disable
cancel.PerformClick()
Me.AutoValidate = Windows.Forms.AutoValidate.Inherit
Return True
End If
Return MyBase.ProcessDialogKey(keyData)
End Function

Thank you for reporting this bug and sorry for introducing any
inconvinience to you.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 18 '06 #5
Hi Jeffrey,
Thank you for researching this for me and thanks for the workaround.
--
Terry
""Jeffrey Tan[MSFT]"" wrote:
Hi Terry,

Sorry for let you wait.

Yes, after performing some research, I found that some customers had
reported the similar issue as a bug. The problem lies in that the ESC key
causes the validating from Button.PerformClick method, while clicking
button will trigger the validating through Control.WmSetFocus internal
method. So it seems that Button.PerformClick method does not check
Button.CauseValidation correctly internally.

Please refer to the bug link below for more information:
"CausesValidation has no effect on Button when PerformClick is called. "
http://connect.microsoft.com/VisualS...k.aspx?Feedbac
kID=116242

To workaround this problem, you may intercept the ESC key explicitly and
set AutoValidate property to AutoValidate.Disable to disable the
validating, sample code snippet is listed below:

Protected Overrides Function ProcessDialogKey(ByVal keyData As
System.Windows.Forms.Keys) As Boolean
Dim cancel As Button = CType(Me.CancelButton, Button)
If keyData = Keys.Escape Then
Me.AutoValidate = Windows.Forms.AutoValidate.Disable
cancel.PerformClick()
Me.AutoValidate = Windows.Forms.AutoValidate.Inherit
Return True
End If
Return MyBase.ProcessDialogKey(keyData)
End Function

Thank you for reporting this bug and sorry for introducing any
inconvinience to you.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 18 '06 #6
You are welcome.

Yes, if you meet further bug-like issue, after performing search in google
without any success, it is a good place to perform search in the MSDN
customer feedback center below:
"Visual Studio and .NET Framework Feedback"
http://connect.microsoft.com/feedbac...spx?SiteID=210

Some general known issues may have been reported by other customers, and
this site will save you a lot of time.

Anyway, if you can not find anything useful in this site, please feel free
to post in newsgroup, I will try my best to help you :-)

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 19 '06 #7
Hi Jeffrey,
Thank you for the link to the feedback site. I went there and searched
for 'escape key' and found the issue - problem is - it says it has been fixed
and the issue is closed. Was there some other place that you meant to direct
me to?
--
Terry
""Jeffrey Tan[MSFT]"" wrote:
You are welcome.

Yes, if you meet further bug-like issue, after performing search in google
without any success, it is a good place to perform search in the MSDN
customer feedback center below:
"Visual Studio and .NET Framework Feedback"
http://connect.microsoft.com/feedbac...spx?SiteID=210

Some general known issues may have been reported by other customers, and
this site will save you a lot of time.

Anyway, if you can not find anything useful in this site, please feel free
to post in newsgroup, I will try my best to help you :-)

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 19 '06 #8
Hi Terry,

Thanks for your feedback!

If I did not misunderstand you, you are talking about the bug link below,
yes?
"Escape key doesn't cancel a form when validation is used "
http://connect.microsoft.com/VisualS...k.aspx?Feedbac
kID=116968

Yes, the description of the customer looks like the same with your problem,
however they are not the same one. I have downloaded the customer's problem
in internal bug database and given it a test. The customer did not set
Button.CauseValidation property to false. So this bug link talks about
another issue in VS2005 beta version which is fixed in VS2005 RTM.

The bug link below is the same issue as yours:
"CausesValidation has no effect on Button when PerformClick is called."
http://connect.microsoft.com/VisualS...k.aspx?Feedbac
kID=116242

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 20 '06 #9

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

Similar topics

3
by: Paul | last post by:
I have an Access 2000 database with a form that is giving me some major headaches. When you open the form, it displays all records and allows editing, but has AllowAdditions set to False so that...
18
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
5
by: Oenone | last post by:
I have a VB.NET form which I'm displaying modally using the ShowDialog() method. Within the form is a Cancel button, and I've set this button into the Form's CancelButton property so that...
32
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of...
131
by: Lawrence D'Oliveiro | last post by:
The "escape" function in the "cgi" module escapes characters with special meanings in HTML. The ones that need escaping are '<', '&' and '"'. However, cgi.escape only escapes the quote character if...
3
by: Josh Kodroff | last post by:
I have a pretty standard Windows Form with 2 relevant buttons: btnOK and btnCancel. I set both buttons' DialogResult properties appropriately. I then set the form AcceptButton and CancelButton...
3
by: Jack Brown | last post by:
Hi there, Has anyone noticed that the "Form.CancelButton" property occasionaly resets itself to "(none)" in the form's property window for no apparent reason (at deisng time using VS 2005). Has...
1
by: Bogdan | last post by:
Hi, I'm trying to programmatically assign a script to a CancelButton's OnClientClick in a wizard step. I'm doing it in ActiveStepChanged event handler. I'm also assigning scripts to finish and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.