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: #####-####");
m_maskEdit.setText("KS662021234");

In ::OnOk()

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

// GetWindowText yields "State: KS, Zip: 66202-1234"
m_maskEdit.GetWindowText(strTemp);

Download demo project - [17] KB

Download source - [2] KB



Comments

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

    Reply
  • maskeditctrl

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

    Originally posted by: subramanian.v

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

    Reply
  • http://www.ucancode.net

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

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

    Cool!

    Reply
  • 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:
    
    

    http://www.exontrol.com/sg.jsp?content=products/exeditors

    Regards,
    Mike

    Reply
  • Excellent Work guys

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

    Originally posted by: fleming george k

    Just what i was looking for ..you guys rock

    Reply
  • how to use in a FormView

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

    Originally posted by: Julia

    Hi,

    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.

    Julia

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

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

    Reply
  • 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;
    GetWindowText(strEditText);

    if (strEditText == m_strSaveText)
    return;

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

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

    CWnd::SetWindowText(m_strSaveText);
    int nNext = gotoNextEntryChar(nStart + 1);
    SetSel(nNext, nNext);
    }
    else
    {
    if (nStart - 1 < 0)
    return;

    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.GetBufferSetLength(nNext);

    m_strSaveText.SetAt(nNext, cReplace);
    m_strSaveText.ReleaseBuffer();
    nNext = gotoNextEntryChar(nNext + 1);
    }
    CWnd::SetWindowText(m_strSaveText);
    SetSel(nNext, nNext);
    }
    }

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

    Thanks.

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

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • 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 …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds