A masked edit control

Download Source Code


I got this masked edit control code from someone (I can't remember whom it was). If anyone wishes to lay claim to the original they are more than welcome. I have found this quite useful. So I can't lay claim to doing all or even most of this. However I have enhanced the code to deal with times as well as dates.

I have also added validation of the keystrokes so that invalid times i.e. > 23:59 are not allowed. (I have limited the time validation to minutes. It would be no great effort to add in the seconds. Validation for dates is a little more difficult. Validation is limited to months < 13 and days < 32.

i.e you can put in 31/02/1998 for example.

Ok how to make it work. There are three classes

  • CMaskEdit
  • CDateEdit
  • CTimeEdit

Include the two files MASKED.CPP and MASKED.H in your project. Also include <afxdao.h> for the COleDateTime features if you already haven't. Modify the include line that says #include "myproj" to point to your project. Create a CEdit control and declare a control variable in your dialog then modify the CEdit declaration in the class header to say either CDateEdit or CTimeEdit

CDateEdit:

Pass your time to CDateEdit with

	CDateEdit myeditctrl;
	myeditctrl.SetDate(COleDateTime var);

and get the edited result back with

	COleDateTime var = myeditctrl.GetDate();

CTimeEdit:

Pass your time to CTimeEdit with

	CTimeEdit myeditctrl;
	myeditctrl.SetTime(COleDateTime var);

and get the edited result back with

	COleDateTime var = myeditctrl.GetTime();

and there you have it.

Please feel free to modify any modifications I have made. These are stated with //added this

Last updated: 15 May 1998



Comments

  • 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
  • Software Engineer

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

    Originally posted by: Larry Reid

    Thanks for the cool control.  May I point out one limitation.  When you do the following, it seems to get confused.
    
    

    COleDateTime oDateTime;
    ASSERT(oDateTime.SetTime(0,0,0)==0);
    m_edtTimeOfDay.SetTime(oDateTime);

    It leaves the edit control blank. The fix is to add/change the following function to:

    {void FormatCOleDateTime(CString& strData, COleDateTime DateTime, int len)
    {
    strData = "";
    if (DateTime.m_dt == 0)
    {
    if(len == 5)
    strData = "00:00";
    }
    else if (len == 8)
    strData = DateTime.Format("%d/%m/%y");
    else if(len == 5) // added these two
    strData = DateTime.Format("%H:%M");
    else
    strData = DateTime.Format("%d/%m/%Y");
    }


    Now she's cool.....

    Reply
  • BUGFIX : About OnChar

    Posted by Legacy on 01/16/1999 12:00am

    Originally posted by: Sang-il, Lee

    Fix OnChar(..)
    
    

    ....
    CEdit::OnChar(nChar, nRepCnt, nFlags);

    if (!m_bMaskKeyInProgress && m_bUseMask && !m_strLiteral.IsEmpty())
    {
    int startPos, endPos;
    GetSel(startPos, endPos);
    // make sure the string is not longer than the mask
    if (endPos < m_strLiteral.GetLength())
    {
    UINT c = m_strLiteral.GetAt(endPos);
    if (c != '_') { // Debuged by Sang-il, Lee T.T
    SetSel(endPos+1,endPos+1);
    }
    }
    }
    }

    Original code doesn't work. (In my visual C++ 6.0)
    Why use SendMessage in SendChar to move cursor.

    this code has bug. if "00--00--00", doesn't work. But
    I have no needs. If you needed, It's your job.

    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 …

  • 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