Re: Filtering characters in a CEdit Box

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



If you are using MFC. Inherit a class form CEdit and do it there.

Something like this:

/////////////// MyEdit.h
#pragma once


// CMyEdit

class CMyEdit : public CEdit
{
DECLARE_DYNAMIC(CMyEdit)

public:
CMyEdit();
virtual ~CMyEdit();

protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
};


// MyEdit.cpp : implementation file
//

#include "stdafx.h"
#include "MyEdit.h"


// CMyEdit

IMPLEMENT_DYNAMIC(CMyEdit, CEdit)
CMyEdit::CMyEdit()
{
}

CMyEdit::~CMyEdit()
{
}


BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
ON_WM_CHAR()
END_MESSAGE_MAP()



// CMyEdit message handlers


void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CString DontWant("<>/|\\");
if (DontWant.Find(nChar) != -1)
{
return;
}
CEdit::OnChar(nChar, nRepCnt, nFlags);
}

Now if you want to handle strings that are pasted into the edit control
also, like David mentioned, I would take over the WM_PASTE message in the
edit control also and handle stripping unwanted chars there.

Then in your dialog that has the edit control attach a CMyEdit to the
control on the dialog box templage

void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT1, m_Edit);
}

class CMainDlg : public CDialog
{
......
private:
CMyEdit m_Edit;
};

Here are a couple of reasons why you shouldn't do it in the EN_CHANGE. First
one is that since you are catching the EN_CHANGE in the parent dialog vs.
catching a reflected message in a CEdit Derived class is that if you want to
have this control in two places then you will endup having to duplicate the
code in two places instead of just including an edit control that knows how
to handle things itself.
Second is the fact that when you handle things in the EN_CHANGE method
things are already done before you get to change things, which means that an
unwanted char is already in the edit control until you take it out with the
call to SetWindowText. Where as catching it in the OnChar you will never
even give it a chance to show up on the screen.


AliR.

"Chris Baker" <ChrisBaker@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:05E47873-8CB1-46CB-BB12-B08341F31D45@xxxxxxxxxxxxxxxx
> Cheers AliR,
>
> I can override the OnChar for the dialog but not the edit box.
>
> In the .NET environment why how can I override this function for the Edit
> Box not the dialog?
>
> Chris
>
> "AliR" wrote:
>
> > Here is what you want to do.
> >
> > Inherit a class form CEdit
> > in there catch the WM_CHAR message. In the message handler put the
following
> > code
> >
> > void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
> > {
> > //things that we don't want
> > CString DontWant("<>/|\\");
> > //if there was one of those then don't process the WM_CHAR
> > if (DontWant.Find(nChar) != -1)
> > {
> > return;
> > }
> > CEdit::OnChar(nChar, nRepCnt, nFlags);
> > }
> >
> >
> > AliR.
> >
> >
> > "Chris Baker" <ChrisBaker@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> > news:5E22A877-6335-4F08-A49C-88A312C8FE9C@xxxxxxxxxxxxxxxx
> > > I have an edit box I am using for entering a new filename, hence I
don't
> > want
> > > the user to enter illegal characters such as < > \ | / and so on.
> > >
> > > I have trapped the EN_CHANGE event and done CString::Replace function
to
> > > remove them.
> > >
> > > Now doing a SetWindowText positions the cursor at the start of the
edit
> > > window, so the text you enter appears backwards.
> > >
> > > So....
> > >
> > > Can I set the cursor to the end of the string?
> > >
> > > Is there a much better way to do the whole thing?
> > >
> > >
> >
> >
> >


.



Relevant Pages

  • Re: SubclassWindow auf eigene EditKlasse
    ... > Habe eine Klasse CCallbackEdit die von CEdit abgeleitet ist. ... > Nun habe ich eine eigene CEdit Klasse CMyEdit. ...
    (microsoft.public.de.vc)
  • Re: Edits & numeric values
    ... Assuming that you have a CEdit derived class for your control, ... CMyEdit which is derived from CEdit or has CEdit as parent. ... Add the OnChar handler to your CMyEdit control and in there you can filter ...
    (microsoft.public.vc.mfc)
  • Re: DLGC_WANTMESSAGE doesnt works well for multi-line edit controls
    ... I wish all keyboard input could be intercepted in my CMyEdit class ... But unfortunately when the edit control is a single-line control, ... Why doesn't DLGC_WANTALLKEYS intercepte all keyboard input? ...
    (microsoft.public.vc.mfc)
  • DLGC_WANTMESSAGE doesnt works well for multi-line edit controls
    ... I don't want ESC to dismiss my dialog box when focus is on the edit ... I wish all keyboard input could be intercepted in my CMyEdit class ... But unfortunately when the edit control is a single-line control, ... Why doesn't DLGC_WANTALLKEYS intercepte all keyboard input? ...
    (microsoft.public.vc.mfc)