Resolved Why errorprovider in datetimepicker is still their after selected and save it?

KenQui

Member
Joined
May 11, 2022
Messages
12
Programming Experience
Beginner
I have three textbox, one datetimepicker, one button for save and one errorprovider.

The problem is every time I input a data in textbox and I forgot to select the date it give an error in datetimepicker then when I select a date because I forgot it then save it the error provider still in their.
the thing I want is after I select a date because it give an error then save it. The error will be gone
Pls help me I cant fine the correct code and because I'm still beginner.
Below is the code that I'm using.

VB.NET:
  Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        If DateTimePicker1.Value.Date < Date.Today Then
            ErrorProvider1.SetError(DateTimePicker1, "DATE SELECTED IS LESS THAN TODAYS DATE")
        Else
            ErrorProvider1.SetError(DateTimePicker1, "PLEASE SELECT DATE TODAY")
        End If
        AddBook()

    End Sub
 
Solution
I would suggest that you're going about this in the wrong way. Validation of WinForms controls should generally be done in the Validating event handler of that control. If the control fails validation then you can set e.Cancel to True and the user will not be able to navigate away from that control until they fix it. In your case, it seems that you'd rather let them navigate away but display an error icon. That's fine too, but you need to make sure that, when the user clicks the Save button, that you validate every control, including the ones that never received focus, and then only act if all controls pass validation. Your code appears to be calling AddBook regardless of whether the data passes validation or...
Where's your code to clear the error? You haven't shown us any and I'd wager you don't have any, so that's why it never gets cleared. the error won't magically clear on its own because the application can't read your mind. If you expect the error to be cleared then you have to write code to clear it and you need to put that code somewhere that it will be executed when you want it executed.
 
I would suggest that you're going about this in the wrong way. Validation of WinForms controls should generally be done in the Validating event handler of that control. If the control fails validation then you can set e.Cancel to True and the user will not be able to navigate away from that control until they fix it. In your case, it seems that you'd rather let them navigate away but display an error icon. That's fine too, but you need to make sure that, when the user clicks the Save button, that you validate every control, including the ones that never received focus, and then only act if all controls pass validation. Your code appears to be calling AddBook regardless of whether the data passes validation or not. Here are two examples of how I might handle validation with an ErrorProvider.
VB.NET:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DateTimePicker1.Value = DateTimePicker1.MinDate
    PerformValidation()
End Sub

Private Sub DataChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged,
                                                                  DateTimePicker1.ValueChanged
    PerformValidation()
End Sub

Private Sub PerformValidation()
    ErrorProvider1.SetError(TextBox1, If(TextBox1.TextLength = 0, "You must enter a value", Nothing))
    ErrorProvider1.SetError(DateTimePicker1, If(DateTimePicker1.Value < Date.Today, "You must select a date no earlier than today", Nothing))

    Button1.Enabled = String.IsNullOrEmpty(ErrorProvider1.GetError(TextBox1)) AndAlso
                      String.IsNullOrEmpty(ErrorProvider1.GetError(DateTimePicker1))
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Perform save here.
End Sub
That will enable the Button if and only if all fields are valid. That state and the state of the ErrorProvider are updated as the user edits the data.
VB.NET:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DateTimePicker1.Value = DateTimePicker1.MinDate
End Sub

Private Sub TextBox1_Validating(sender As Object, e As CancelEventArgs) Handles TextBox1.Validating
    ErrorProvider1.SetError(TextBox1, If(TextBox1.TextLength = 0, "You must enter a value", Nothing))
End Sub

Private Sub DateTimePicker1_Validating(sender As Object, e As CancelEventArgs) Handles DateTimePicker1.Validating
    ErrorProvider1.SetError(DateTimePicker1, If(DateTimePicker1.Value < Date.Today, "You must select a date no earlier than today", Nothing))
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Ensure that even controls that have not received focus are validated.
    ValidateChildren()

    'Only act if all fields are valid.
    If String.IsNullOrEmpty(ErrorProvider1.GetError(TextBox1)) AndAlso
       String.IsNullOrEmpty(ErrorProvider1.GetError(DateTimePicker1)) Then
        'Perform save here.
    Else
        MessageBox.Show("Valid data must be entered in all fields before saving")
    End If
End Sub
The Button is always enabled in that case, so a message is displayed if the operation cannot proceed due to invalid data. The ErrorProvider state is updated only when the user navigates away from a control or clicks the Button.
 
Solution
Here the code but i cant fine the correct code for it.

VB.NET:
   If DateTimePicker1.Value.Date = Not Selected Then
            ErrorProvider1.SetError(DateTimePicker1, "PLEASE SELECT A DATE.")
            Return
        Else
            Me.ErrorProvider1.SetError(Me.DateTimePicker1, "")
        End If
 
I would suggest that you're going about this in the wrong way. Validation of WinForms controls should generally be done in the Validating event handler of that control. If the control fails validation then you can set e.Cancel to True and the user will not be able to navigate away from that control until they fix it. In your case, it seems that you'd rather let them navigate away but display an error icon. That's fine too, but you need to make sure that, when the user clicks the Save button, that you validate every control, including the ones that never received focus, and then only act if all controls pass validation. Your code appears to be calling AddBook regardless of whether the data passes validation or not. Here are two examples of how I might handle validation with an ErrorProvider.
VB.NET:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DateTimePicker1.Value = DateTimePicker1.MinDate
    PerformValidation()
End Sub

Private Sub DataChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged,
                                                                  DateTimePicker1.ValueChanged
    PerformValidation()
End Sub

Private Sub PerformValidation()
    ErrorProvider1.SetError(TextBox1, If(TextBox1.TextLength = 0, "You must enter a value", Nothing))
    ErrorProvider1.SetError(DateTimePicker1, If(DateTimePicker1.Value < Date.Today, "You must select a date no earlier than today", Nothing))

    Button1.Enabled = String.IsNullOrEmpty(ErrorProvider1.GetError(TextBox1)) AndAlso
                      String.IsNullOrEmpty(ErrorProvider1.GetError(DateTimePicker1))
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Perform save here.
End Sub
That will enable the Button if and only if all fields are valid. That state and the state of the ErrorProvider are updated as the user edits the data.
VB.NET:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DateTimePicker1.Value = DateTimePicker1.MinDate
End Sub

Private Sub TextBox1_Validating(sender As Object, e As CancelEventArgs) Handles TextBox1.Validating
    ErrorProvider1.SetError(TextBox1, If(TextBox1.TextLength = 0, "You must enter a value", Nothing))
End Sub

Private Sub DateTimePicker1_Validating(sender As Object, e As CancelEventArgs) Handles DateTimePicker1.Validating
    ErrorProvider1.SetError(DateTimePicker1, If(DateTimePicker1.Value < Date.Today, "You must select a date no earlier than today", Nothing))
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Ensure that even controls that have not received focus are validated.
    ValidateChildren()

    'Only act if all fields are valid.
    If String.IsNullOrEmpty(ErrorProvider1.GetError(TextBox1)) AndAlso
       String.IsNullOrEmpty(ErrorProvider1.GetError(DateTimePicker1)) Then
        'Perform save here.
    Else
        MessageBox.Show("Valid data must be entered in all fields before saving")
    End If
End Sub
The Button is always enabled in that case, so a message is displayed if the operation cannot proceed due to invalid data. The ErrorProvider state is updated only when the user navigates away from a control or clicks the Button.
THANK VERY MUCH , I GOT IT :)
 

Latest posts

Back
Top