Search code examples
visual-c++mfcmfc-feature-pack

How can I validat each character entered by the user in MFC property grid control (CMFCPropertyGridCtrl)


I have been trying to validate and update each character in the properties edit box (CMFCPropertyGridCtrl) which was entered by the user.I searched the MSDN and find function like PushChar() etc. But those methods didn't solve my problem. Basically I need to implement CEdit::OnChar() function for the CMFCPropertyGridCtrl edit boxes.
enter image description here


Solution

  • I am going to give the sample code for this. In CustomProperties.h, Derive a class form CMFCPropertyGridProperty

    class CMyEditProp : public CMFCPropertyGridProperty
    {
    public:
        CMyEditProp (const CString& strName, const CString& strValue, LPCTSTR lpszDescr = NULL, DWORD dwData = 0);
    
    protected:
        virtual CWnd* CreateInPlaceEdit(CRect rectEdit, BOOL& bDefaultFormat);
        virtual CString FormatProperty();
    };  
    

    Also derive a class from CEdit and implement a OnChar() method in it.

    class MyEdit:public CEdit
    {
    public: 
            void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
            {
              if(!IsCharAlpha(nChar))
                return;
    
              CEdit::OnChar(nChar, nRepCnt, nFlags);
            }
      DECLARE_MESSAGE_MAP()
    }; 
    

    In CustomProperties.cpp Implement all the methods which were declared in header file.

    CMyEditProp ::CPasswordProp(const CString& strName, const CString& strValue, LPCTSTR lpszDescr, DWORD dwData)
    : CMFCPropertyGridProperty(strName, (LPCTSTR) strValue, lpszDescr, dwData)
    {
    }
    
    CWnd* CMyEditProp ::CreateInPlaceEdit(CRect rectEdit, BOOL& bDefaultFormat)
    {
        MyEdit pWndEdit;
        DWORD dwStyle = WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL ;
    
        if (!m_bEnabled || !m_bAllowEdit)
        {
            dwStyle |= ES_READONLY;
        }
    
        pWndEdit.Create(dwStyle, rectEdit, m_pWndList, AFX_PROPLIST_ID_INPLACE);
    
    
        bDefaultFormat = TRUE;
        return &pWndEdit;
    }
    
    BEGIN_MESSAGE_MAP(MyEdit,CEdit)
        ON_WM_CHAR()
    END_MESSAGE_MAP()
    

    This will work like as Edit control and you can validate all the characters entered by the user.