A form has many buttons. One of those buttons contains code to update the TableAdapterManager. One of the many TextBox controls has code in a Validating event handler. There is code to make sure a US phone number is formatted properly.
If the user tabs out of the TextBox the Validating code works perfectly and show a message to the user if the phone number is not properly formatted and the focus is place in the offending TextBox.
If the user clicks on a button that has code to update the TableAdapterManager the Validating code fires but instead of focus remaining on the offending TextBox, the code in the button Click handler also fires.
I would like to stop the button code from firing.
Here is the code for the Validating event of the TextBox:
Private Sub TextBoxPrimaryPhone_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBoxPrimaryPhone.Validating
' Make sure the phone is formatted correctly.
'--------------------------------------------
If PhoneFormat(TextBoxPrimaryPhone.Text) = "Fix Phone Number" Then
' Alert the user.
'----------------
MessageBox.Show("Please enter a 7 or 10 digit phone number.", _
"Entry Error", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
e.Cancel = True
Else
' Format according to the length of the phone number entered by the user.
'------------------------------------------------------------------------
TextBoxPrimaryPhone.Text = PhoneFormat(TextBoxPrimaryPhone.Text)
End If
End Sub
What additional coding to I need to include so focus remains on the TextBox?
After e.Cancel = True is called do this to set the focus of the offending textbox:
In your button click event handler you can do this: