?
Solved

MaskedTextBox - Testing For Dirty (Returns Error If Blank)

Posted on 2012-03-28
13
Medium Priority
?
560 Views
Last Modified: 2012-03-28
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
0
Comment
Question by:Clif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
13 Comments
 
LVL 5

Expert Comment

by:Rainverse
ID: 37778424
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
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37778441
if(txtValue.Text.Trim().Length==0)
return Nothing
else
return CDate(txtValue.Text)
0
 
LVL 10

Author Comment

by:Clif
ID: 37778442
I'm not sure how that will allow me to compare the value in txtRegExpiry with the value in VehicleList(i).Reg_Expr
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:Rainverse
ID: 37778464
The problem occurrs when you try to compare a blank value to a date, correct?
0
 
LVL 10

Author Comment

by:Clif
ID: 37778472
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
 
LVL 10

Author Comment

by:Clif
ID: 37778490
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
 
LVL 5

Expert Comment

by:Rainverse
ID: 37778494
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
 
LVL 5

Expert Comment

by:Rainverse
ID: 37778499
Mom-date.... Oops. Typing on a phone. Non-date
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 2000 total points
ID: 37778502
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
 
LVL 5

Expert Comment

by:Rainverse
ID: 37778508
He's saying the same thing. Just different way.
0
 
LVL 10

Author Comment

by:Clif
ID: 37778536
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
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37778555
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
 
LVL 10

Author Closing Comment

by:Clif
ID: 37778670
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

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question