Solved

MaskedTextBox - Testing For Dirty (Returns Error If Blank)

Posted on 2012-03-28
13
551 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now