MaskedTextBox - Testing For Dirty (Returns Error If Blank)

Using VB.Net 2010 (Pro)

I have a form that contains various textboxes, comboboxes and a couple of maskedtextboxes.  These last ones are giving me fits.

The value in the maskedtextboxes are all formatted as shortdate.  It's possible for them to be empty (no date).  Occassionally there will be a date that the user must remove (make it blank).

I'd like to show if a form is "dirty", that is if the user has changed the data from the last time it was loaded or saved.  To that end, I have a structure where I keep all the values from the various controls on the form.  Everything works fine except for those darned maskedtextboxes, when they are empty.  During the checkForDirty() routine, I get to a line such as:
bDirty = bDirty Or CDate(txtRegExpiry.Text) <> IIf(IsNothing(.Reg_Expr), "", .Reg_Expr)

Open in new window

which, if the maskedtextbox is empty will throw the error:
Conversion from string "  /  /" to type 'Date' is not valid.
Note: It is the control (CDate(txtRegExpiry.Text)) that is specfically throwing the error.

I tried removing the CDate() but that didn't help.

So, the question is, how do I compare a maskedtextbox to a variable that can either be a date or nothing without throwing an error?

TIA
LVL 10
ClifAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RainverseCommented:
You should be able to test the value before evaluating it, using the MaskFull property:

If (myMaskedTextBox.MaskFull) Then
    ' Filled out....
Else
    ' Not filled out....
End If

Open in new window


-MJC
0
BuggyCoderCommented:
if(txtValue.Text.Trim().Length==0)
return Nothing
else
return CDate(txtValue.Text)
0
ClifAuthor Commented:
I'm not sure how that will allow me to compare the value in txtRegExpiry with the value in VehicleList(i).Reg_Expr
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

RainverseCommented:
The problem occurrs when you try to compare a blank value to a date, correct?
0
ClifAuthor Commented:
BuggyCoder

Your example gives basically the same error.

The value of an empty MaskedTextBox that is formatted as shortdate is: "/  /"  (at least that is what is it in this case)
0
ClifAuthor Commented:
Rainverse,

The problem occurs (at this point) while I'm comparing the value in a MaskedTextBox named txtRegExpiry with a value in a variable named VehicleList(i).Reg_Expr and the MaskedTextBox is "empty" (which appears, based on my prior post, to mean the prompt is still there, but there is no other values).
0
RainverseCommented:
Right MaskFull will tell you if it's been fiilled out or not. You can't compare a date to a mom-date so first you determine whether it's a date at all. If it is, then you do your comparison.
0
RainverseCommented:
Mom-date.... Oops. Typing on a phone. Non-date
0
BuggyCoderCommented:
see what i wanted to tell you was check for the length of value in masked text box, if it is greater than 0 then only do the comparison otherwise do some default action as per your system requirements...

if(txtMasked.Text.Length>0)
//do some nice stuff here
else
//move out of here
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RainverseCommented:
He's saying the same thing. Just different way.
0
ClifAuthor Commented:
That's all well and good, except that a partial fill (ie, month and day but no year) in the MaskedTextBox will show up as equal to the empty variable (that is, not dirty), which is not correct.
0
BuggyCoderCommented:
use DateTime.TryParse to see if the value is valid datetime or not....
see an example here:-
http://www.dotnetperls.com/datetime-tryparse
http://www.dotnetperls.com/datetime-tryparseexact
0
ClifAuthor Commented:
Rather than going for length, I decided to go for literal.  I'm testing for "/  /".  I'm sure if we ever go international, I'll have to revisit this, but for the time begin it now works.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.