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.

- 91
- 1
- 13
-
1Is it not possible to override PreTranslateMessage and process the messages for WM_CHAR there? According to the documentation it supports the PTM method: https://msdn.microsoft.com/en-us/library/bb983759.aspx – Andrew Truckle May 09 '16 at 08:35
-
`CEdit::OnChar()` is not what you're looking for, nor is its `CMFCPropertyGridCtrl` equivalent. Both would fail to account for input coming from sources other than the keyboard (e.g. a mouse paste operation). You need to respond to change notifications. – IInspectable May 09 '16 at 09:11
-
@IInspectable: I think you can't do it via OnChar, even when it comes from keyboard, because it is being overridden by its own `CMFCPropertyGridCtrl::PreTranslateMessage` – sergiol May 09 '16 at 21:55
2 Answers
When you provide lpszEditMask
or lpszEditTemplate
or lpszValidChars
parameters to the constructor of a property, the edit control of that property will be a CMFCMaskedEdit
instead of a normal CEdit
. You can confirm what I said on CMFCPropertyGridProperty::CreateInPlaceEdit
implementation.
So, if I was in your place, I would read CMFCMaskedEdit
documentation to know how to input the masking, put a breakpoint on CMFCPropertyGridProperty::CreateInPlaceEdit
to see how it behaves when you supply the lpsz...
arguments and then would come back here to the page to report how had things gone.

- 4,122
- 4
- 47
- 81
-
CMFCPropertyGridProperty::CreateInPlaceEdit is working fine for me – Suman Reddy May 17 '16 at 10:20
-
`CMFCPropertyGridProperty::CreateInPlaceEdit` is a virtual function. Did you override it on a derived class made by you? – sergiol May 18 '16 at 07:40
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.

- 91
- 1
- 13