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...

	#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());
or
	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

Updates

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:00am

    Originally posted by: Brian Norman

    Has anyone managed to use this control with multiline option?

    Thanks

    Brian Norman

    Reply
  • Bug :- you can paste invalid charcters

    Posted by Legacy on 04/15/2002 12:00am

    Originally posted by: RGidvani

    You can paste invalid characters in this masked edit control

    Reply
  • uninitialised members in CMaskEdit class

    Posted by Legacy on 10/10/2001 12:00am

    Originally 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()
    {
    m_isdate = FALSE;
    m_bisTime = FALSE;
    ...
    }

    Reply
  • Unable to download source

    Posted by Legacy on 10/09/2001 12:00am

    Originally 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:00am

    Originally 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!!

    Reply
  • Help formating Masked edit Control !!!

    Posted by Legacy on 03/17/2000 12:00am

    Originally 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!!

    Reply
  • Capitalization Solution

    Posted by Legacy on 09/20/1999 12:00am

    Originally 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.

    Reply
  • Capitalization option would be nice

    Posted by Legacy on 07/29/1999 12:00am

    Originally 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.

    Reply
  • bug with "9"

    Posted by Legacy on 10/22/1998 12:00am

    Originally 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
    'if (nChar != VK_SPACE) return TRUE;' to
    'if (nChar == VK_SPACE) return TRUE;'

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds