Introduction
Ever wanted to edit tree or list control labels, and include text validation as you type? I searched a lot of places for such methods of validating text while typing in the label edit control of a tree or list control on an MFC dialog. I could only find complicated custom classes and the like, most of which had far more functionality than I was after.
Method
I implemented something very simple in the dialog PreTranslateMessage
method (use the Class Wizard to add this method to your dialog code). Whether the following code is useful to you depends on the complexity of the validation you require. I was only interested in restricting input, so this works very well.
BOOL CCMyDialog::PreTranslateMessage(MSG* pMsg)
{
CString strEditIn = "";
CString strEditOut = "";
TCHAR c;
CWnd *pCont = GetFocus();
CEdit *pEdit = m_ListCtrl.GetEditControl();
if (pCont == pEdit)
{
switch(pMsg->message)
{
case WM_CHAR:
switch(pMsg->wParam)
{
case VK_RETURN:
case VK_DELETE:
case VK_BACK:
case VK_ESCAPE:
case VK_LEFT:
case VK_RIGHT:
case VK_UP:
case VK_DOWN:
case VK_SHIFT:
case VK_CONTROL:
::TranslateMessage(pMsg);
::DispatchMessage(pMsg);
break;
default:
c = pMsg->wParam;
strEditIn = c;
strEditIn.MakeUpper();
strEditOut = ValidateText(strEditIn, 1,
FALSE);
if (strEditIn != strEditOut)
{
pMsg->wParam = 0;
}
::TranslateMessage(pMsg);
::DispatchMessage(pMsg);
break;
}
return (TRUE);
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
However, bear in mind that some of the pre-defined virtual key codes can conflict with other character codes. For instance, VK_DOWN
equals (0x28) (40), which is the same code as the open bracket '(' character. Normally, the down character isn't that important in single line Edit controls, but if you do need to include 'down' and the '(' character, you would have to use the WM_KEYDOWN
message to record which key has actually been pressed.
For alphanumeric text, you could try the following:
BOOL CCMyDialog::PreTranslateMessage(MSG* pMsg)
{
CString strEditIn = "";
CString strEditOut = "";
TCHAR c;
BOOL bPassThru = FALSE;
CWnd *pCont = GetFocus();
CEdit *pEdit = m_ListCtrl.GetEditControl();
if (pCont == pEdit)
{
switch(pMsg->message)
{
case WM_KEYDOWN:
if ((pMsg->wParam <= VK_HELP) ||
(pMsg->wParam >= VK_LWIN))
{
bPassThru = TRUE;
}
case WM_CHAR:
switch(pMsg->wParam)
{
case VK_BACK:
::TranslateMessage(pMsg);
::DispatchMessage(pMsg);
break;
default:
if (!bPassThru)
{
c = pMsg->wParam;
strEditIn = c;
strEditIn.MakeUpper();
strEditOut = ValidateText(strEditIn,
1, FALSE);
if (strEditIn != strEditOut)
{
pMsg->wParam = 0;
}
::TranslateMessage(pMsg);
::DispatchMessage(pMsg);
return (TRUE);
}
}
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
Note this is not meant to be an article on methods of validating text. There are plenty of such methods available on line.
Revision History
12 Aug 2002 - Initial Revision.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.