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.