OnValidating and Focus

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've researched this for a couple of hours 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
 
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.
 
Back
Top