|
-
Jun 23rd, 2022, 03:13 PM
#1
Thread Starter
Fanatic Member
[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?
-
Jun 23rd, 2022, 03:19 PM
#2
Re: Date Validation in textbox
Please post the code you are using for this, along with the exact text present in the textbox.
-
Jun 23rd, 2022, 03:25 PM
#3
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.
-
Jun 23rd, 2022, 04:04 PM
#4
Thread Starter
Fanatic Member
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
-
Jun 23rd, 2022, 04:06 PM
#5
Thread Starter
Fanatic Member
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.
-
Jun 23rd, 2022, 04:19 PM
#6
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.
-
Jun 23rd, 2022, 04:23 PM
#7
Thread Starter
Fanatic Member
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.
-
Jun 23rd, 2022, 04:24 PM
#8
Re: Date Validation in textbox
 Originally Posted by gwboolean
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.
-
Jun 23rd, 2022, 04:40 PM
#9
Thread Starter
Fanatic Member
Re: Date Validation in textbox
I had already attempted that. Same error.
-
Jun 23rd, 2022, 04:48 PM
#10
Re: Date Validation in textbox
 Originally Posted by gwboolean
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
-
Jun 23rd, 2022, 04:56 PM
#11
Thread Starter
Fanatic Member
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.
-
Jun 23rd, 2022, 06:40 PM
#12
Re: Date Validation in textbox
 Originally Posted by gwboolean
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)
-
Jun 23rd, 2022, 07:29 PM
#13
Thread Starter
Fanatic Member
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.
-
Jun 23rd, 2022, 07:41 PM
#14
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
 
-
Jun 23rd, 2022, 07:47 PM
#15
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.
-
Jun 23rd, 2022, 07:52 PM
#16
Thread Starter
Fanatic Member
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.
-
Jun 23rd, 2022, 08:18 PM
#17
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
 
-
Jun 24th, 2022, 11:56 AM
#18
Thread Starter
Fanatic Member
Re: [RESOLVED] Date Validation in textbox
 Originally Posted by Shaggy Hiker
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.
-
Jun 24th, 2022, 12:12 PM
#19
Thread Starter
Fanatic Member
Re: Date Validation in textbox
 Originally Posted by wes4dbt
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.?
-
Jun 24th, 2022, 12:24 PM
#20
Re: Date Validation in textbox
 Originally Posted by wes4dbt
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
-
Jun 24th, 2022, 01:03 PM
#21
Re: Date Validation in textbox
 Originally Posted by gwboolean
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.
-
Jun 24th, 2022, 01:08 PM
#22
Thread Starter
Fanatic Member
Re: Date Validation in textbox
 Originally Posted by wes4dbt
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|