A masked edit control | CodeGuru

A masked edit control

Download Source Code I got this masked edit control code from someone (I can’t remember whom it was). If anyone wishes to lay claim to the original they are more than welcome. I have found this quite useful. So I can’t lay claim to doing all or even most of this. However I have enhanced […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 1998
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Download Source Code

I got this masked edit control code from someone (I can’t remember whom
it was). If anyone wishes to lay claim to the original they are more than welcome.
I have found this quite useful. So I can’t lay claim to doing all or even most of this.
However I have enhanced the code to deal with times as well as dates.

I have also added validation of the keystrokes so that invalid times i.e. >
23:59 are not allowed. (I have limited the time validation to minutes. It would be no
great effort to add in the seconds. Validation for dates is a little more difficult.
Validation is limited to months < 13 and days < 32.

i.e you can put in 31/02/1998 for example.

Ok how to make it work. There are three classes

  • CMaskEdit
  • CDateEdit
  • CTimeEdit

Include the two files MASKED.CPP and MASKED.H in your project.
Also include <afxdao.h> for the COleDateTime features if you already
haven’t. Modify the include line that says #include “myproj” to point to your
project. Create a CEdit control and declare a control variable in your dialog
then modify the CEdit declaration in the class header to say either
CDateEdit or CTimeEdit

CDateEdit:

Pass your time to CDateEdit with

	CDateEdit myeditctrl;
	myeditctrl.SetDate(COleDateTime var);

and get the edited result back with

	COleDateTime var = myeditctrl.GetDate();

CTimeEdit:

Pass your time to CTimeEdit with

	CTimeEdit myeditctrl;
	myeditctrl.SetTime(COleDateTime var);

and get the edited result back with

	COleDateTime var = myeditctrl.GetTime();

and there you have it.

Please feel free to modify any modifications I have made. These are stated
with //added this

Last updated: 15 May 1998

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.