Results 1 to 7 of 7

Thread: VB.net WinForms Control Validation not working

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    92

    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
    Name:  Untitled.jpg
Views: 801
Size:  32.2 KB
    Attached Images Attached Images  
    Last edited by wjburke2; Feb 10th, 2020 at 05:16 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    92

    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
    Last edited by wjburke2; Feb 11th, 2020 at 08:50 AM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    92

    Re: VB.net WinForms Control Validation not working

    Thank you
    Last edited by wjburke2; Feb 11th, 2020 at 08:51 AM.

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: VB.net WinForms Control Validation not working

    Quote Originally Posted by wjburke2 View Post
    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    92

    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
    Last edited by wjburke2; Feb 11th, 2020 at 02:39 PM.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: VB.net WinForms Control Validation not working

    You need to look in the CancelEventArgs...

    Name:  11-02-2020_20.32.12.png
Views: 667
Size:  10.8 KB

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width