Solved

MaskedTextBox - Testing For Dirty (Returns Error If Blank)

Posted on 2012-03-28
13
555 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
  • 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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 500 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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