Permitting Abort using Error Provider Control

E

Ed Bitzer

I am trying to ascertain the value of the Error Provider Control and
it does identify the control failing validation without any extra code
but I cannot figure how to abort if the user throws in the towel and
cannot correct during the session. The following is my basic
structure and EmailAddressOK is simply and function assessing whether
or not the content of TextBox1 is valid. Move off TextBox1 and the
Validating sub takes charge and will not let go<g>. Once found
invalid how do I abort?

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim address As String = ""
Dim msg As String = ""
Try
If Not EmailAddressOK(TextBox1.Text, address, msg) Then
' Cancel the event and select the text to be corrected
by the user.
e.Cancel = True
TextBox1.Select(0, TextBox1.Text.Length)
' Set the ErrorProvider error with the text to
display.
Me.ErrorProvider1.SetError(TextBox1, msg)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub TextBox2_Validated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox2.Validated
Me.ErrorProvider1.SetError(TextBox2, "")
End Sub

Appreciate,
Ed
 
C

Cor Ligthert[MVP]

Ed,

Set it in the validating it to not showing the error provider as there is no
error as you do in the validated event.
(Or simple start with that)

Cor
 
E

Ed Bitzer

Cor,

If I do that say with a button click, the Validating event occurs
first and the code never gets to the button click even and definitely
not the close instruction there. Where should I place that
instruction?

Ed
 
C

Cor Ligthert[MVP]

Ed,

Are you sure of that? Strange I thought that the Buttons did not change the
focus.
If that was like that, then the EndEdit or EndCurrentEdit would not be
needed.

Cor
 
J

Jack Jackson

Set the CausesValidation property of the Cancel button to False. That
will allow that button to be pressed without validation occuring.

You might still have problems with closing a form via the Windows
close button. If so there are other techniques to use for that.
 
E

Ed Bitzer

Jack: Turned the CausesValidation to False and I can perform other
functions within the button click event a simple Me.Close is never
performed with the Validating subroutine called next in the sequence.
There has got to be some value here with the persistence of the
exclamation marker and the retention of the error message but this
novice (but retired<g>) is getting discouraged when he knows a lost
focus event and a MsgBox with resonse options will do the job.
Ed
 
E

Ed Bitzer

Cor: As Jack pointed out I had left the CausesValidation property on
the button True which certainly redirected. However you that
correction still did not permit Me.Close. I have used the Search
function of Visual Studio for information on EndEdit and
EndCurrentEdit but the help I located was associated with releasing
data binding in grids.
Ed
 
C

Cor Ligthert[MVP]

Ed,

In my idea does a button click not let a control lose focus, therefore the
validating will not be called.

That is why I wrote about the endedit end the endcurrentedit, that is to
force that leaving of a control.


Cor
 
E

Ed Bitzer

Cor, Appreciate your sticking with this old retired guy still
stubbornly struggling with programming (almost 30 yrs<g>). To review
I have two textboxes with CausesValidation set to True and of course
an ErrorProvider. A buttons CausesValidation is now set to False. If
I move from one textbox to the second, leaving an error behind, the
ErrorValidator takes action, displays the problem and leaves the small
exclamation point in the red circle adjacent to the textbox. Now if I
attempt to abort by pressing a button with that purpose, without
fixing the error, Debug shows the next code step is the button and
steps through content where I can delete the contents of the
textboxes, clear the ErrorValidators with
Me.ErrorProvider1.SetError(TextBox1, "") and
Me.ErrorProvider1.SetError(TextBox2, "") but when I reach Me.Close()
the program jumps to the Sub TextBox1_Validating(.....) and I am
stuck in the loop.

I assume endedit and endcurretedit are methods but I have no idea how
to implement.

Ed
 

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