Click here to Skip to main content
13,553,855 members
Click here to Skip to main content


250 bookmarked
Posted 29 Jul 2004
Licenced CPOL

CFilterEdit: Use Regular Expressions to Filter Your Input

, 3 Oct 2011
The definitive approach to filtering input text. Includes configurable error display.
#if !defined(AFX_FILTEREDIT_H__222BE616_C621_4D59_A5F8_B69FCA4D4D50__INCLUDED_)
#define AFX_FILTEREDIT_H__222BE616_C621_4D59_A5F8_B69FCA4D4D50__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// FilterEdit.h : header file

#ifndef _tstoi
#ifdef _UNICODE
#define _tstoi _wtoi
#define _tstof atof
#define _tstoi atoi

#include <boost/regex.hpp>

#define TTS_BALLOON 0x40

namespace FilterEdit
#ifdef _UNICODE
typedef boost::wregex CRegEx;
typedef boost::regex CRegEx;

// CBaseEdit window
class CBaseEdit : public CEdit
// Operations
	CBaseEdit (const bool bProcessChars = true);
	CBaseEdit (const TCHAR *pszRegEx, const bool bProcessChars = true);
	CBaseEdit (CRegEx *pRegEx, const bool bProcessChars = true);
	virtual ~CBaseEdit ();

	bool SetRegEx (const TCHAR *pszRegEx);
	void SetRegEx (CRegEx *pRegEx);
	bool IsInputValid (const bool bSetInputValid = true);
	const CString &GetProposedText () const;
	virtual void Increment (const int /*iInc*/) {};

	void SetProcessChars (const bool bProcessChars);
	bool GetProcessChars () const;
	void SetAllowEmpty (const bool bAllowEmpty);
	bool GetAllowEmpty () const;

	static void EscapeString (CString &str);

	// Display related functions
	void SetWavyLineOnError (const bool bWavyLineOnError);
	bool GetWavyLineOnError () const;
	void SetWavyLineColour (const COLORREF crWavyLine);
	void SetBackgroundColourFocus (const COLORREF crBkFocus);
	void SetForegroundColourFocus (const COLORREF crFgFocus);
	void SetBackgroundColourOK (const COLORREF crBkOK);
	void SetForegroundColourOK (const COLORREF crFgOK);
	void SetBackgroundColourError (const COLORREF crBkError);
	void SetForegroundColourError (const COLORREF crFgError);

	// Tooltip functions
	void CreateToolTip (CWnd *pParent, const TCHAR *pszText,
		const int iIconType = 1, const TCHAR *pszTitle = _T("Data Format"));
	void SetToolTipText (const TCHAR *pszText);
	void SetToolTipTitle (const int iIconType, const TCHAR *pszTitle);
	void SetDisplayTTonNull (const bool bDisplayTTonNull);
	bool GetDisplayTTonNull () const;
	bool GetToolTipCreated () const;

	bool IsTextValid (const CString &strText);
	virtual bool SyntaxCheck (const CString &strText);
	virtual bool SemanticCheck (const CString &strText,
		CString *pstrErrorMsg = 0);
	bool ValidateChar (UINT nChar, const bool bDisplayToolTip = true);
	bool Match (const CString &strText, const bool bDisplayToolTip = true);

	bool CanDelete ();
	bool CanPaste ();
	void ResetValid ();
	void SetInputValid (const bool bInputValid);

	void DisplayToolTip (const bool bDisplay, const TCHAR *pszText = 0);

// Overrides
	// ClassWizard generated virtual function overrides
	virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);

	// Generated message map functions
	afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
	afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
	afx_msg void OnKillFocus(CWnd* pNewWnd);
	afx_msg void OnSetFocus(CWnd* pOldWnd);
	afx_msg HBRUSH CtlColor(CDC *pDC, UINT nCtlColor);
	afx_msg void OnPaint();


	bool m_bProcessChars;
	bool m_bAllowEmpty;
	bool m_bRegExOwner;
	CRegEx *m_pRegEx;

	bool m_bInputValid;
	bool m_bControlDown;
	CString m_strProposedText;
	CString m_strPrevToolTipText;

	// Display related members
	bool m_bWavyLineOnError;
	COLORREF m_crWavyLineColour;
	COLORREF m_crFgFocus;
	COLORREF m_crBkFocus;
	COLORREF m_crFgError;
	COLORREF m_crBkError;
	CBrush m_brFocus;
	CBrush m_brOK;
	CBrush m_brError;
	bool m_bDisplayTTonNull;
	CToolTipCtrl m_ToolTip;

	void SetDefaults ();
	void ConstructRegEx ();

	CBaseEdit (CBaseEdit const &); // No copy construction.
	CBaseEdit &operator = (CBaseEdit const &); // No assignment.


// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_FILTEREDIT_H__222BE616_C621_4D59_A5F8_B69FCA4D4D50__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ben Hanson
Software Developer (Senior)
United Kingdom United Kingdom
I started programming in 1983 using Sinclair BASIC, then moving on to Z80 machine code and assembler. In 1988 I programmed 68000 assembler on the ATARI ST and it was 1990 when I started my degree in Computing Systems where I learnt Pascal, C and C++ as well as various academic programming languages (ML, LISP etc.)

I have been developing commercial software for Windows using C++ for 22 years.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 3 Oct 2011
Article Copyright 2004 by Ben Hanson
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid