Masked Edit Control (3)

We had been using the MSMask.ocx control in our MFC application for almost a year. Recently a 200K memory leak was traced to the setText method (200K each time it was called), so we downloaded another masked edit class from the web, but it flickered intensely and the code was kind of icky. Anyway, I decided to write my own. Turns out it works better than I could have hoped. It's easy to use and the source code very simple allowing anyone to extend it as necessary.

In ::OnInitDialog()
m_maskEdit.SubclassDlgItem(IDC_EDIT_MASK, this);

// Initialize the control to "State: KS, Zip: 66202-1234"
m_maskEdit.setMask("State: ??, Zip: #####-####");

In ::OnOk()

// getText returns "KS66202123"
CString strTemp = m_maskEdit.getText();

// GetWindowText yields "State: KS, Zip: 66202-1234"

Download demo project - [17] KB

Download source - [2] KB


  • Kleiner Bug ?

    Posted by mobo2000 on 01/26/2005 03:39am

    I have tested a simple example "##,##". When I insert a ',' as the 4. character then this character will be discard, but the insertion point of edit control goes to next character. I think, this ist not a good way. I havn't test other examples. Could be this behaviour is the default for your edit control.

  • maskeditctrl

    Posted by Legacy on 08/30/2003 12:00am

    Originally posted by: subramanian.v

    i dont know from which class u derived the setMask() methods.
    plz mail me.
    i m inurgent.


    Posted by Legacy on 07/11/2003 12:00am

    Originally posted by: XD++ MFC Class Library with Source Code!


  • ExEditors Library

    Posted by Legacy on 03/19/2002 12:00am

    Originally posted by: Mike Philips

    A complete collection of editors in a single file:


  • Excellent Work guys

    Posted by Legacy on 01/25/2002 12:00am

    Originally posted by: fleming george k

    Just what i was looking for guys rock

  • how to use in a FormView

    Posted by Legacy on 05/08/2001 12:00am

    Originally posted by: Julia


    I find it is very useful for me. However, I failed in useing it in a FormView. How to do it?
    It would be very much appreciated if anyone could give me instruction on this.


  • Pasting Problem?

    Posted by Legacy on 11/04/1999 12:00am

    Originally posted by: Simon Beck

    After trying the demo I noticed that if you paste text into the edit control, only the last character of pasted text is actually displayed.

  • Deleting text

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

    Originally posted by: Johnny D

    When the selection is hilighted and the user hit the delete key, the whole thing should be deleted.

  • RE: Minor changes....

    Posted by Legacy on 05/18/1999 12:00am

    Originally posted by: Catalin A. Culiniac

    Here is the code for OnUpdate():
    void CMaskEdit::OnUpdate()
    int nStart, nEnd;
    GetSel(nStart, nEnd);

    CString strEditText;

    if (strEditText == m_strSaveText)

    if (m_bLastKeyWasBackspace)
    m_strSaveText.SetAt(nStart, isMaskChar(nStart) ? m_strMaskText[nStart] : m_cPlaceHolder);

    int nNext = gotoNextEntryChar(nStart);
    SetSel(nNext, nNext);
    else if (m_bLastKeyWasDelete)
    m_strSaveText.SetAt(nStart, isMaskChar(nStart) ? m_strMaskText[nStart] : m_cPlaceHolder);

    int nNext = gotoNextEntryChar(nStart + 1);
    SetSel(nNext, nNext);
    if (nStart - 1 < 0)

    int nNext = gotoNextEntryChar(nStart - 1);
    char cWanted = strEditText[nStart - 1];
    char cReplace = 0;

    if (getReplacementChar(nNext, cWanted, cReplace))
    if (m_strSaveText.GetLength() < nNext)

    m_strSaveText.SetAt(nNext, cReplace);
    nNext = gotoNextEntryChar(nNext + 1);
    SetSel(nNext, nNext);

  • Minor changes needed!

    Posted by Legacy on 11/20/1998 12:00am

    Originally posted by: Victor Boctor

    After trying the demo application that you included, I wanted to share wth you my comments:

    1. If the user clicks a key which is not compatible with the current character of the mask, the cursor is moved to the next cell. I believe the cursor should not be moved in this case. I would recommend also that a virtual function should be added with to handle such invalid keys.

    2. In the example you have the first token is a constant, thus when the user clicks a key the cursor moves to the first editable character and then the second key is written there! I believe the first key should be written.


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

Top White Papers and Webcasts

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there's simply not enough quality talent to go around. In response, companies often resort to inferior solutions -- hiring substandard developers and engineers, recruiting talent on a part-time or temporary basis, poaching people from competitors, or burdening an already stressed IT staff for more of their labor. Fortunately, there's a better solution. Read this white paper to learn the business value of …

  • As the mobile enterprise marketplace expands and customer needs grow more diverse, Samsung recognizes that solution partners and developers play an essential role by continually innovating to meet their customers' needs. Samsung works to provide these developers and partners with the latest tools and resources needed to create these solutions. Read this program guide to learn how the Samsung Enterprise Alliance Program provides partners and developers with Samsung enterprise software development kits (SDKs) …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date