2 Attachment(s)
VB.net WinForms Control Validation not working
I have a form that has a Datetime picker which is bound to a SQL data source. I am trying to validate the control. I have put simple code in the events Leave, Validating, Validated and ValueChanged. They appear to fire in the order I have listed them. When I look at the value in the dtpSL_Login_Date.value at each break point it does not contain the updated value from the control until its gets to the valueChanged event. Is there a secret to get the changed value during the validate event. It seems weird that VS 2015 has a Validate event but the program can not access the updated value. What would one validate the load value? If not has someone figured out a work around.
Code:
If dtpSL_Login_Date.value = Save_SL_LogIn_Date Then
Return
End If
Attachment 174419
Re: VB.net WinForms Control Validation not working
Don't post pictures of code. Code is text. Post it as text and format it as code. That way, we can read it easily and we can also copy and paste it. Also, provide steps that we can reproduce your issue, e.g. tell us what you entered where and when and what values you expected to see where and when and what values you actually see. The Validating event works for thousands of people the world over so, if it's not working for you, either you're doing something wrong or something is broken on your system. Either way, we can't diagnose the issue without knowing exactly what you're doing.
Re: VB.net WinForms Control Validation not working
Actually this is a known bug. MSDN was able to recreate the issue and has given me a work around. One must disable and then enable the control in order to get the program buffer to update. My final code is below. JM, Thanks for your reply. I know you have much experience and I always value your input.
Code:
Private Sub dtpSL_LogIn_Date_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles dtpSL_LogIn_Date.Validating
' Fires on leave the control with/without changes
' Fires when record changes
' The program buffer does not contain the value in the control
' Disable and enable the control seems to get the current value
dtpSL_LogIn_Date.Enabled = False
dtpSL_LogIn_Date.Enabled = True
If dtpSL_LogIn_Date.Value = Save_SL_LogIn_Date Then
Exit Sub
End If
End Sub
Re: VB.net WinForms Control Validation not working
Re: VB.net WinForms Control Validation not working
Quote:
Originally Posted by
wjburke2
Actually this is a known bug. MSDN was able to recreate the issue and has given me a work around. One must disable and then enable the control in order to get the program buffer to update. My final code is below. JM, Thanks for your reply. I know you have much experience and I always value your input.
Code:
Private Sub dtpSL_LogIn_Date_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles dtpSL_LogIn_Date.Validating
' Fires on leave the control with/without changes
' Fires when record changes
' The program buffer does not contain the value in the control
' Disable and enable the control seems to get the current value
dtpSL_LogIn_Date.Enabled = False
dtpSL_LogIn_Date.Enabled = True
If dtpSL_LogIn_Date.Value = Save_SL_LogIn_Date Then
Exit Sub
End If
End Sub
You say this code works the way you want? I must be missing something because it doesn't look like that code does anything. No matter what the value in the dtp it just exits the Validating sub without doing anything.
Re: VB.net WinForms Control Validation not working
You are right, I just put that in there so I could stop the program and check the value. My final code ended up being
Code:
Private Sub dtpSL_LogIn_Date_Validating(sender As Object, e As ComponentModel.CancelEventArgs) Handles dtpSL_LogIn_Date.Validating
' Fires on leave the control with/without changes
' The control value dose not contain the value in the control
' Disable and Enable the control seems to get the current value
dtpSL_LogIn_Date.Enabled = False
dtpSL_LogIn_Date.Enabled = True
' Fires when record changes (This skips validate on position change)
If tbSL_Shift_Key.Text <> Save_SL_ShiftKey Then
Exit Sub
End If
' Only want to validate if value changed.
If dtpSL_LogIn_Date.Value <> Save_SL_LogIn_Date Then
Validate_LogIn_Date()
End If
End Sub
1 Attachment(s)
Re: VB.net WinForms Control Validation not working
You need to look in the CancelEventArgs...
Attachment 174465
Setting e.Cancel = True if an invalid value is entered won't allow the user to leave the control until a valid value is entered