Solved

MaskedTextBox - Testing For Dirty (Returns Error If Blank)

Posted on 2012-03-28
13
559 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 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