Resolved Is there a better way instead of ErrorProvider

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
I can validate a textbox's value whether it's has or not and then event of saving button does its task to pass the data through database.
At the point I wonder there is a better method to do that? or How can I stop the save button event to run if that validation condition occurs fail?
VB.NET:
Dim bValidateZipCode As Boolean
Private Sub txtZipCode_Leave(sender As Object, e As EventArgs) Handles txtZipCode.Leave
        If Not IsNumeric(txtZipCode) OrElse (txtZipCode.TextLength = Nothing) Then
            ErrorProvider1.SetError(txtZipCode, "Provide a zip code")
        Else
            bValidateZipcode = True
        End If
    End Sub
 
Last edited:
Hi,

You would use the Validating Event of a TextBox to correctly ensure that a Value has been entered into the TextBox. If your conditional checks identify invalid information you just set e.Cancel to True which prevents leaving the TextBox until the correct conditions are met. i.e:-

VB.NET:
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
  If TextBox1.Text = String.Empty Then
    e.Cancel = True
    'Show Error Provider information etc.
  End If
End Sub

Hope that helps.

Cheers,

Ian
 
Hi,
You would use the Validating Event of a TextBox to correctly ensure that a Value has been entered into the TextBox. If your conditional checks identify invalid information you just set e.Cancel to True which prevents leaving the TextBox until the correct conditions are met. i.e:-
Hope that helps.
Cheers,
Ian
Hi Ian,
Thanks for your feedback. I like it and will use it. That method is awesome, it does not let you click anywhere until provide a valid value.
But How can I deactivate ErrorProvider icon's appearing around of that control after its got a valid value.

-----
I found it by myself thanks again Ian,
VB.NET:
Else
  ErrorProvider1.Dispose()
 
Last edited:
Hi,
You just call ErrorProvider1.Clear() when you want to remove the error notification icon.
Hope that helps.
Cheers,
Ian
I see, thanks. But let's just say that there are two controls and they skipped to do something and press on the save button then a sql server extension thrown out that one of those controls value needs to pass through database because its not null property, then how to validate this scenario?
 
Hi,

To accommodate this scenario you will need to use a Nested IF statement in the Save Button to test for the correct valid conditions for each TextBox. If all the TextBox's pass the conditional validation then commit the Save to the Database otherwise show the ErrorProvider on the offending TextBox and set the Focus to the offending TextBox. You could then use this method to replace the Validating Events of the TextBox's or you could use this additional check in conjunction with the TextBox Validating events.

Hope that helps.

Cheers,

Ian
 
Socarsky said:
How can I deactivate ErrorProvider icon's appearing around of that control after its got a valid value.
help said:
To clear the error message, call the SetError method and pass in Empty for the String value. This removes the error glyph from the specified Control.
IanRyder said:
You just call ErrorProvider1.Clear() when you want to remove the error notification icon.
Note that will also reset error messages for all other associated controls.
Socarsky said:
But let's just say that there are two controls and they skipped to do something
Then you can use this form/container method: ContainerControl.ValidateChildren Method (System.Windows.Forms)
 
I am sorry for saying these words but something does not work expected. I faced issue about ValidateChildren.
Private Sub btnFSave_Click(sender As Object, e As EventArgs) Handles btnFSave.Click
        If ValidateChildren() Then
            If (sConn.State = ConnectionState.Closed) Then
                sConn.Open()
            End If
            Switch()            ' to set switchValue's value true=1 false=0
            beforeSave()        ' checking some controls before saving
            Try
                Dim getdate As String = "getdate()"
                 Dim strInsert As String = "INSERT INTO tbFirm VALUES(" & intInputInterest.Text & " ,'" &  Format(Me.dtpAgreement.Value, "yyyy-MM-dd") & "'); SELECT SCOPE_IDENTITY() AS nFirmID"
                Dim SqlCmdForTbFirm As New SqlCommand(strInsert, sConn)
                Dim getReturnFirmID As Short = CShort(SqlCmdForTbFirm.ExecuteScalar())
                 Dim strInsertGroups As String = "INSERT INTO tbFirmGroups VALUES("  & getReturnFirmID & ",'" & txtBtnFGrp1.Text & "')"
                Dim SqlCmdForTbFirmGroups As New SqlCommand(strInsertGroups, sConn)
                SqlCmdForTbFirmGroups.ExecuteNonQuery()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Else
             MessageBox.Show("You must check some controls that whether input or not  " & vbCrLf & "because program needs those controls provided  data")
        End If
    End Sub


If I click on the save button and trigger the event of button control causes ValidateChildren() once for txtFDescription.text validating not the other 4 more validating controls.It means if provide a valid data in txtFDescription and then click on the save button one more but ValidateChildren does not work after once usage as interestingly even I could not provided data for the other 4 controls.

Private Sub txtZipCode_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtZipCode.Validating
        If txtZipCode.Text = String.Empty Then
            e.Cancel = True
            ErrorProvider1.SetError(txtZipCode, "Provide a valid zip code!")
        Else
            ErrorProvider1.Dispose()
        End If
    End Sub

    Private Sub txtFDescription_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtFDescription.Validating
        If txtFDescription.Text = String.Empty Then
            e.Cancel = True
            ErrorProvider1.SetError(txtFDescription, "Provide a valid description!")
        Else
            ErrorProvider1.Dispose()
        End If
    End Sub
 
Hi,

What you have to remember is that when you call ValidateChildren it will run the code in ALL the controls that have a Validating event which includes controls where the validation conditions are correctly met. Therefore, the way you have currently coded these events at the moment is to Dispose the ErrorProvider if ANY ONE of the validation events are successful. This therefore removes ALL ErrorProvider notifications.

To do this correctly I would suggest that you remove the code in the Validating events which Disposes the ErrorProvider. You could then place the removal of the ErrorProvider notification in, lets say, the TextBox TextChanged event using a single routine which handles the same TextChanged Event for all the TextBoxes. i.e:-

VB.NET:
Private Sub TextBox1_Enter(sender As Object, e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
  ErrorProvider1.SetError(DirectCast(sender, TextBox), String.Empty)
End Sub

Here I use the Sender object to Reset the Error notification on a single specific TextBox by setting the SetError method of the ErrorProvider to an Empty String.

My only other comment would be to use Parameters to insert values into your SQL statements in your first block of code rather than using string concatenation. If you have a look at the signature of one of jcmilhinney's posts you can use his Blog link to get a good example of how to do this along with the reasons for doing so.

Hope that helps.

Cheers,

Ian
 
Dispose is the last call you make to an object (that implements IDisposable) before throwing it in the garbage bin and never refer to it again. That's the one and only time you call that method.
 
Back
Top