Re: limiting edit box characters

From: Bill Thompson (billt61_at_rgv.rr.com)
Date: 12/24/04


Date: Fri, 24 Dec 2004 01:20:41 -0600


"Jayw710" <jayw710@aol.com> wrote in message
news:20041224005303.08274.00002469@mb-m05.aol.com...
> Hi,
>
> Is there a way to limit what characters an edit box will accept? I could
do
> keyboard intercept but that seems like overkill since there are a million
> controls on the page and they all have different characters they can
accept. I
> looked for an event for edit box that tells when a new character is
entered,
> but couldn't find that either. Is there a standard way to do this?
>
> E.g. I just want to limit my chars to the set to all digits 0-9 plus the
period
> and comma. I want to add commas automatically if the user doesn't.
>
> Ideas?
>
> Thank you:)
>
> Jay
>

Derive a class from CEdit. Add a WM_CHAR event handler. Use the derived
class for your edit controls. Note: when you define a variable for a edit
control from the dialog definition screen, create a Control type, NOT a
String type. Then, replace the CEdit type definition for the controls with
your cedit-derived class name.

You can use GetWindowText to get the string data out of your cedit-derived
class object. This implies you don't need to use UpdateData, which is kind
of a pain in the neck anyway.

You can create your new cedit-derived class using the class wizard, choose
New Class..., and select CEdit as the base class.

As a further enhancement, you could add a member function to your
cedit-derived class to specify different character masking behaviors; call
the member function from your dialog's constructor for each instance of the
new edit controls. Alternatively, you could derive different classes with
different behaviours.

If you find that the WM_CHAR event handler isn't sufficient to cover your
needs, consider working with WM_KEYDOWN as well.

Here's an example:

// CLASS DEFINITION

class CMyEdit : public CEdit
{
// Construction
public:
    CMyEdit();

// Attributes
public:

// Operations
public:

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyEdit)
    //}}AFX_VIRTUAL

// Implementation
public:
    virtual ~CMyEdit();

    // Generated message map functions
protected:
    //{{AFX_MSG(CMyEdit)
    afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()
};

// CLASS IMPLEMENTATION

CMyEdit::CMyEdit()
{
}

CMyEdit::~CMyEdit()
{
}

BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
    //{{AFX_MSG_MAP(CMyEdit)
    ON_WM_CHAR()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

// note that we only pass numeric characters through to the cedit base class
// effectively ignoring any character that is not '0', '1'...'9'

void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    if (nChar >= '0' && nChar <= '9')
        CEdit::OnChar(nChar, nRepCnt, nFlags);
}



Relevant Pages

  • Re: limiting edit box characters
    ... plz find "Edit controls" in the left column. ... Good luck. ... > controls on the page and they all have different characters they can ...
    (microsoft.public.vc.language)
  • Re: dynamically creating a list of controls
    ... I would use a grid control, ... make the first cell a edit, the second a combo, and third and forth an edit. ... out a series of controls, ...
    (microsoft.public.vc.mfc)
  • Re: Greatest strengths
    ... My unit is probably a chapter and as they are short I summarise ... action or the words but the idea flow if you like. ... I think that's still true even in edit - you might ... contains (ideas I had before I knew the characters well). ...
    (rec.arts.sf.composition)
  • Re: Wave 3 makes the Hotmail UI as convenient as punch cards
    ... Only by whatever code is implemented within an e-mail client. ... use vertical bar characters if it wanted to. ... But what happens is that when you try to edit ... Maybe you should consider using the Bcc field if your recipients are to ...
    (microsoft.public.internet.mail)
  • Re: SetThemeAppProperties and SetWindow Theme do not effect Unicode data handling
    ... I am quite sure that is not true that controls in Windows 2000 and on always ... the Windows multibyte codepages. ... national language characters in the upper half of the 256 character ... stopped working for our Japanese customers. ...
    (microsoft.public.win32.programmer.ui)