Results 1 to 22 of 22

Thread: [RESOLVED] Date Validation in textbox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Resolved [RESOLVED] Date Validation in textbox

    While executing the IsDate() method in a routine I found the following error.

    Text = Cannot evaluate expression because the code of the current method is optimized.
    While I have been able to find some explanations of optimization, all of which are beyond my level, I really have no understanding of what this means or how to approach dealing with it.

    What I am doing is just validating a date input to a textbox. I do know of other methods to do this, but it was my understanding that using the IsDate() method was quite simple. Is there perhaps a tool or library required?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: Date Validation in textbox

    Please post the code you are using for this, along with the exact text present in the textbox.

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Date Validation in textbox

    you can write your own

    Code:
     Dim myDate As New DateTime(2022, 6, 23)
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            'MessageBox.Show(DateOk("13.13.21"))
            MessageBox.Show(DateOk(myDate))
        End Sub
    
        Public Function DateOk(input As String) As Boolean
            Dim result As DateTime
            Return DateTime.TryParse(input, result)
        End Function
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Date Validation in textbox

    Lost my internet for awhile.

    Sure, here is the routine from which it came.

    Code:
        Private Sub txtSubmit_Leave(sender As Object, e As EventArgs) Handles txtSubmit.Leave
            'Date Validation
            If String.IsNullOrEmpty(txtSubmit.Text) Then Exit Sub
            If Not IsDate(txtSubmit) Then
                MsgBox("Please enter a valid date.", vbCritical)
                txtSubmit.SelectionStart = 0
                txtSubmit.SelectionLength = Len(txtSubmit)
                txtSubmit.Focus()
            End If
        End Sub

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Date Validation in textbox

    Chris,

    Thanks. It is turning out that there are a lot of ways to skin this cat. Anything is better than counting characters.

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: Date Validation in textbox

    Code:
    If Not IsDate(txtSubmit) Then
    VB.NET doesn't have default properties for objects like VB6 did, so textbox will never be a date.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Date Validation in textbox

    Well that is a bummer. I thought about seeing if there was a tool to be able to use that, but probably the best thing is to just take a pass on that method and move on to other options.

    Thanks.

  8. #8
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: Date Validation in textbox

    Quote Originally Posted by gwboolean View Post
    Well that is a bummer. I thought about seeing if there was a tool to be able to use that, but probably the best thing is to just take a pass on that method and move on to other options.

    Thanks.
    ...Or you could just fix your code to reference the .Text property like you do in the line above it. Up to you.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Date Validation in textbox

    I had already attempted that. Same error.

  10. #10
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: Date Validation in textbox

    Quote Originally Posted by gwboolean View Post
    I had already attempted that. Same error.
    Well, it needs to be txtSubmit.Text if you want that line to function as I imagine you intend it to.

    That said, the error message you indicated in your first post is discussed in the below thread. I can't offer any further assistance, good luck.

    https://stackoverflow.com/questions/...d-is-optimized

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Date Validation in textbox

    Thanks for the link. I have looked through the content and I can tell that I will have to study it awhile. But good to find material that appears to relate exactly with what is occurring.

  12. #12
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Date Validation in textbox

    Quote Originally Posted by gwboolean View Post
    Lost my internet for awhile.

    Sure, here is the routine from which it came.

    Code:
        Private Sub txtSubmit_Leave(sender As Object, e As EventArgs) Handles txtSubmit.Leave
            'Date Validation
            If String.IsNullOrEmpty(txtSubmit.Text) Then Exit Sub
            If Not IsDate(txtSubmit) Then
                MsgBox("Please enter a valid date.", vbCritical)
                txtSubmit.SelectionStart = 0
                txtSubmit.SelectionLength = Len(txtSubmit)
                txtSubmit.Focus()
            End If
        End Sub
    there are a few errors in your code, you should write IsDate(txtSubmit.text) instead of IsDate(txtSubmit). same for Len(txtSubmit).

    with these corrections, I tested it and it works (on VS2017, Framework 4.5) without any problem
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Date Validation in textbox

    I will test out your corrections. I appreciate that. Although it will be a while before I can get to that. While trying to mess with a couple of debugger things that I should never touch on pain of death, I have pretty much screwed the pooch on this and it will take a few hours to rebuild the form/code. But as soon as I get everything back up and running I will give it a try.

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Date Validation in textbox

    First off, if you use a DateTimePicker, then whatever it holds will always be a date (unless you set it to hold just time).

    Second, the initial error usually happens when you are debugging the Release version of a program, which isn't necessarily a good idea. For a long time, the optimizer didn't seem to do all that much, so it didn't seem to matter which version of the program you were debugging, but that has changed starting with possibly VS2019. In Project Properties on the Compile tab, there is a button for Advanced Compile Options down near the bottom of the page. That brings up a window that has a checkbox on it for Enable Optimizations. That will be checked by default on Release builds, and unchecked by default on Debug builds. If you have it checked, then the code that gets generated will optimize away anything that it can. In this case, it optimized away enough that when an exception happened, it couldn't decide what to show you.

    Debug with optimization off.
    My usual boring signature: Nothing

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

    Re: Date Validation in textbox

    I have to ask, why are you using the TextBox "Leave" event??

    Validation should be done in the "Validating" event.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Date Validation in textbox

    Delany,

    Wow, weird but cool. Problem solved!!! First and foremost, I followed your suggestions. However, before I could try out your corrections, my computer crashed and all of those problems I had created, while messing around with the debugger were magically gone. I immediately put in your corrections and it works. Thanks a lot!

    Shaggy!

    Not a fan of DateTimePickers. I know they are good, but I just like to input the date. I was actually set in dubugging instead of release. One of the things that I messed with and should not have.

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [RESOLVED] Date Validation in textbox

    I'm not thrilled with the DTP, either, though it has some really nice features if you are reaching back in time. I haven't seen anything I like better, though. Dates are difficult.
    My usual boring signature: Nothing

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: [RESOLVED] Date Validation in textbox

    Quote Originally Posted by Shaggy Hiker View Post
    I'm not thrilled with the DTP, either, though it has some really nice features if you are reaching back in time. I haven't seen anything I like better, though. Dates are difficult.

    True Dat.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Date Validation in textbox

    Quote Originally Posted by wes4dbt View Post
    I have to ask, why are you using the TextBox "Leave" event??

    Validation should be done in the "Validating" event.
    I am using the leave event because it is the event that it fits the way I am using the form. The date fields are only used as a reference point for specific points in a process and to close off pieces of the process after they are completed. All of the inputs on the form are sequential and have tab Index/Stops set. At least those I use.

    I already know that Leave does not fit all of the conditions that I would want to invoke the routine. The Validation event I do not really understand very well, but I would be happy to give it a try, if I really knew what it was doing. Leave I just fully understand and that is really my primary reason for using it.

    I get the impression that if I used it instead of Leave that some of the shortcomings I have been experiencing with Leave might go away? Would the Validation Event cover Enter, Click, et. al.?

  20. #20
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Date Validation in textbox

    Quote Originally Posted by wes4dbt View Post
    I have to ask, why are you using the TextBox "Leave" event??

    Validation should be done in the "Validating" event.
    Yep.

    Code:
        Private Sub TextBox1_Validating(sender As Object,
                                        e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    
            If Not Date.TryParse(TextBox1.Text, Nothing) Then
                TextBox1.Text = Date.MaxValue.ToShortDateString
                TextBox1.SelectAll()
                e.Cancel = True
            End If
    
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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

    Re: Date Validation in textbox

    Quote Originally Posted by gwboolean View Post
    I am using the leave event because it is the event that it fits the way I am using the form. The date fields are only used as a reference point for specific points in a process and to close off pieces of the process after they are completed. All of the inputs on the form are sequential and have tab Index/Stops set. At least those I use.

    I already know that Leave does not fit all of the conditions that I would want to invoke the routine. The Validation event I do not really understand very well, but I would be happy to give it a try, if I really knew what it was doing. Leave I just fully understand and that is really my primary reason for using it.

    I get the impression that if I used it instead of Leave that some of the shortcomings I have been experiencing with Leave might go away? Would the Validation Event cover Enter, Click, et. al.?
    The Validating event is not complicated and dbasnett has provided you with an example. Take a few minutes and learn how to use it. With the Validating event the control doesn't ever loose focus and you don't have to reenter the control.

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Date Validation in textbox

    Quote Originally Posted by wes4dbt View Post
    The Validating event is not complicated and dbasnett has provided you with an example. Take a few minutes and learn how to use it. With the Validating event the control doesn't ever loose focus and you don't have to reenter the control.

    Thanks, I will do that. I appreciate the help.

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