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

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds