Updated : 12th August 1998
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...
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.
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.
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);
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);
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);
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
Last updated: 12 August 1998