A masked edit control (2)
Download
Demo Project and Source (37k) or Download Source
Only (4k)
Updated : 12th August 1998
The original source for this code came from Sam Claret's article A Masked Edit Control.
CTimeEdit
The class that I was originally interested in was his CTimeEdit that allowed the editing
of hours and minutes in an edit control. His original code only allowed a maximum 23rd
hour, and 59th minute, i.e. the standard 24 hour clock. There was no way of
specifying a 12 hour clock or even a 48 hour clock.. For this reason I have added SetHours(int hrs), SetMins(int mins)to
CTimeEdit to allow this, and SetTime(CString Date),
and CString GetTimeStr() to extend the use with
other time functions..
As with Sam's code...
or#include <afxdao.h>in a header file such as stdafx.h. Then create a CEdit box in the dialog editor, and using ClassWizard declare a CEdit control variable for it such as m_EditCtrl.Next edit the relevant header file to change the control variable from CEdit to CTimeEdit.
In a fuction such as OnInitDialog either write...
m_EditCtrl.SetTime(COleDateTime::GetCurrentTime());m_EditCtrl.SetTime("11:03");To set the time and then to set the maximum hours and minutes write...
m_EditCtrl.SetHours(12); m_EditCtrl.SetMins(59);To return the time in a string rather than a COleDateTime object write...
CString string = m_EditCtrl.GetTimeStr();
The default maximum hours are 24, and the default maximum minutes are 59.
CMaskEdit
Joe Ismert asked me how he could use this masked edit class to allow entry of,
for example a telephone number. Well CDateEdit and CTimeEdit are both sub-classes of
CMaskEdit. To create your own customised masked edit control you can either create your
own sub-class of CMaskEdit for your own unique mask, or use CMaskEdit directly. The
following Telephone Number, IP Address, and Post Code examples all use CMaskDirectly,
however can be placed a their own unique sub-class for which either CDateEdit and
CTimeEdit can be used as examples.
Telephone Number
Create a CMaskEdit control called for example m_EditPhoneCtrl. Then in OnInitDialog write...
//CMaskEdit - Telephone Initialisation m_EditPhoneCtrl.m_bisTime = FALSE; //For use with CTimeEdit m_EditPhoneCtrl.m_isdate = FALSE; //For use with CDateEdit m_EditPhoneCtrl.m_bUseMask = TRUE; //Set to use CMaskEdit m_EditPhoneCtrl.m_strMask = "0000 0000000"; //The mask string m_EditPhoneCtrl.m_strLiteral = "____ _______"; //"_" char = character entry //" " char = no character entry m_EditPhoneCtrl.m_str = 0116 2111111"; //Initial value m_EditPhoneCtrl.m_strMaskLiteral = m_EditPhoneCtrl.m_str; //Backspace replace string m_EditPhoneCtrl.SetWindowText(m_EditPhoneCtrl.m_str);IP Address
Create a CMaskEdit control called for example m_EditIPCtrl. Then in OnInitDialog write...
//CMaskEdit - IP Address Initialisation m_EditIPCtrl.m_bisTime = FALSE; m_EditIPCtrl.m_isdate = FALSE; m_EditIPCtrl.m_bUseMask = TRUE; m_EditIPCtrl.m_strMask = "999.999.999.999"; m_EditIPCtrl.m_strLiteral = "___.___.___.___"; m_EditIPCtrl.m_str = "209.66 .99 .126"; m_EditIPCtrl.m_strMaskLiteral = m_EditIPCtrl.m_str; m_EditIPCtrl.SetWindowText(m_EditIPCtrl.m_str);Post Code
Create a CMaskEdit control called for example m_EditPCodeCtrl. Then in OnInitDialog write...
//CMaskEdit - IPost Code Initialisation
m_EditPCodeCtrl.m_bisTime = FALSE; m_EditPCodeCtrl.m_isdate = FALSE; m_EditPCodeCtrl.m_bUseMask = TRUE; m_EditPCodeCtrl.m_strMask = "LL00 0LL"; m_EditPCodeCtrl.m_strLiteral = "____ ___"; m_EditPCodeCtrl.m_str = "LE12 7AT"; m_EditPCodeCtrl.m_strMaskLiteral = m_EditPCodeCtrl.m_str; m_EditPCodeCtrl.SetWindowText(m_EditPCodeCtrl.m_str);
CMaskEdit m_strMask
The member variable m_strMask specifies what type of characters can be written and where.
0 Digit only 9 Digit or space # Digit or space or + or - L Alpha only ? Alpha or space A Alphanumeric a Alphanumeric or space & All printable characters
12th August 1998
CMaskEdit : Added the use of backspace to replace what has already
been typed with another string (by Rodney Fujimoto).
The examples use the original string as a replacement, however by default the
replacement is blank, thus nothing will be replaced by the backspace key.
Provided separate .zip file of masked.cpp and masked.h to demo .zip file.
Updated demo .exe.
7th August 1998
CTimeEdit : Added SetTime(CString Date), and CString GetTimeStr().
CMaskEdit : Cured bug with "9" masks not allowing spaces.
Updated article and demo .exe, added examples.
27th July 1998
Original Version
Last updated: 12 August 1998

Comments
Multiline???
Posted by Legacy on 04/04/2003 12:00amOriginally posted by: Brian Norman
Has anyone managed to use this control with multiline option?
Thanks
Brian Norman
ReplyBug :- you can paste invalid charcters
Posted by Legacy on 04/15/2002 12:00amOriginally posted by: RGidvani
You can paste invalid characters in this masked edit control
Replyuninitialised members in CMaskEdit class
Posted by Legacy on 10/10/2001 12:00amOriginally posted by: Tino11
the members m_isdate and m_bisTime of the class CMaskEdit should be initialised in the constructor.
In debug-mode all runs perfect, but if you start it in a Release-generated project, the inputs may be randomized.
like this:
CMaskEdit::CMaskEdit()
Reply{
m_isdate = FALSE;
m_bisTime = FALSE;
...
}
Unable to download source
Posted by Legacy on 10/09/2001 12:00amOriginally posted by: sshan
There seems to be some problem in downloading the source.
I am getting an empty zip file which doesn't contain anything.
Reply
Help formating Masked edit Control !!!
Posted by Legacy on 03/17/2000 12:00amOriginally posted by: Paco Castro
Hi!, I'm from M�xico and I've some troubles formating numbers with this control, I would like to justify numbers to the right as they should be, but I can't (they all go to the left). And I don't know how can I do it , or which controls can I use.
please help me!!
ReplyHelp formating Masked edit Control !!!
Posted by Legacy on 03/17/2000 12:00amOriginally posted by: Paco Castro
Hi!, I'm from M�xico and I've some troubles formating numbers with this control, I would like to justify numbers to the right as they should be, but I can't (they all go to the left). And I don't know how can I do it , or which controls can I use.
please help me!!
ReplyCapitalization Solution
Posted by Legacy on 09/20/1999 12:00amOriginally posted by: Brant Thomsen
An easy way to force capitalization (either upper or lower) is to change the style of the edit box in the resources. There are options to force either upper case or lower case.
ReplyCapitalization option would be nice
Posted by Legacy on 07/29/1999 12:00amOriginally posted by: Matt Cecile
It would be nice to have alphabetic masks that force alphabetic characters to upper or lower case.
Also, the m_str field must currently be at least as long as the mask or memory exceptions occur when the user types over the end. Probably, there should be a routine to set m_str and it should pad the strings to ensure this.
Replybug with "9"
Posted by Legacy on 10/22/1998 12:00amOriginally posted by: Chrubi
There is a bug with mask "9". To correct this one you need to modify line 282 in source "Masked.cpp" from
Reply'if (nChar != VK_SPACE) return TRUE;' to
'if (nChar == VK_SPACE) return TRUE;'