Validation and Focus

G

Guest

(Sorry if this post shows up twice; the first attempt seems not to have been
successful, so I've reposted.)

Hi,

I've researched this for quite a while now, and can't find a satisfactory
answer. Perhaps someone has an idea. The original context of this problem is
a data entry control which checks to see if changes have been saved when some
other control is activated. If not, the user is asked if he wants to save,
discard or cancel (continue editing). To make it easier to reproduce I've
simplified the setup...

1. Take a form with two textboxes and a button. The button moves focus from
Textbox1 to Textbox2. No problem.

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


2. Add validation code to Textbox1. Let's say the text must be "a",
otherwise focus will not be transferred. No problem, this works as expected:
we can't leave the control until an "a" is entered.

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If TextBox1.Text <> "a" Then
e.Cancel = True
Exit Sub
End If
End Sub


3. Now we want to give the user the chance to move to Textbox2 anyway - the
user knows best, after all. So display a confirmation message box.

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If TextBox1.Text <> "a" Then
If MsgBox("Are you sure?", MsgBoxStyle.YesNo) = MsgBoxResult.No
Then
Debug.Print(Me.Name & ": OnValidating failed")
e.Cancel = True
Exit Sub
End If
End If
End Sub


This is where the problem is.

With the keyboard, everything works fine. When trying to move from Textbox1
to Textbox2 by keyboard (tab key) when there is no "a" in Textbox1, I get the
message box. If I click on No, focus remains on Textbox1. If I select Yes,
focus moves to Textbox2, in spite of the wrong value. OK.

Same thing if I click on Textbox2 directly: I get the confirmation message
and land in either T1 or T2.

But if I try to move by means of the Button on the form, it doesn't quite
work. Selecting No in the message box returns focus to Textbox1, that's fine.
But, alas, selecting Yes just puts the focus on the Button, not on Textbox2
where I want it!

I believe I have at least part of the explanation, but I'm not very happy
with it and I can't find a workaround or any other way to get what I want to
work the way I want it to...

Clicking on the Button causes Textbox1's validating event to fire, but the
button's Click event is "lost" through the interruption caused by the message
box and the code in Button1.Click is never executed.

Why is this and how can I achieve what I want?


I hope my problem has become clear. I can't imagine that I'm the only one
trying to do this; this must have been encountered and solved before, so I
really hope to get some valuable tips here...

Thanks
Tom
 
C

ClayB

Try setting Button1.CausesValidation = False to see if that gives you
the behavior you want. This property setting makes it so clicking
TextBox1 does not trigger the validation process on the Control losing
focus.
===============
Clay Burch, .NET MVP
Syncfusion, Inc.
 
J

Jeffrey Tan[MSFT]

Hi Tom,

Yes, your explanation is correct. The problem is mainly caused by the
validation while you clicking the Button.

To resolve this problem, your logic should disable the validation of Button
control. Just as Clay pointed out, you may set Button1.CausesValidation =
False to disable the Button control validation. Based on my test, it works
well in your simple reproduce scenario.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Clay, Jeffrey,

thanks to both of you. Such a simple solution - and really quite sensible; I
really should have found it myself, with a bit of consideration. Sometimes
you just have to step back and think about what's really happening.

Anyway, now I can get on with my work....

Thanks again
Tom
 
J

Jeffrey Tan[MSFT]

Hi Tom,

Glad to see the solution makes sense to you. Yes, we sometimes miss
something quite simple.

Anyway, if you need further help, plaese feel free to post, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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