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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds