Mixed Case Edit Control

–>

The VC++ dialog editor allows you to have "any", "lower", and
"upper" case characters typed into a CEditCtrl. A user of one of my apps
commented that the displaying of some CEditCtrl data looked untidy, they were either lower
or upper case, or even a mixture of the two cases. What was required was an an edit box
that would force the left most character typed into it to upper case. If for example,
e-d-i-t were to be typed, the edit box would interpret and display it as E-d-i-t. This was
very easy to do by sub-classing CEditCtrl and monitoring all WM_CHAR messages in
PreTranslateMessage. One should not however if the characters were to be typed into the
edit control, and the first character then deleted, the old second character will not
automatically become upper case. I did not require this within the CMyEditCtrl class,
however perhaps could be a exercise for someone else.

To use :

  • #include "MyEditCtrl.h" in the header file of the file that you wish to use it
    in.
  • Add MyEditCtrl.cpp and MyEditCtrl.h to your project workspace
  • Create an edit box in the dialog editor.
  • Create a CMyEditCtrl variable in class wizard for the edit box.
// MyEditCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "MyEditCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyEditCtrl

CMyEditCtrl::CMyEditCtrl()
{
}

CMyEditCtrl::~CMyEditCtrl()
{
}


BEGIN_MESSAGE_MAP(CMyEditCtrl, CEdit)
    //{{AFX_MSG_MAP(CMyEditCtrl)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyEditCtrl message handlers
BOOL CMyEditCtrl::PreTranslateMessage(MSG* pMsg)
{
 // TODO: Add your specialized code here and/or call the base class
 if (pMsg->message == WM_CHAR)
 {
  int start, end;
  GetSel(start, end);

  if (start == 0)
  pMsg->wParam = toupper(pMsg->wParam);
 }

 return CEdit::PreTranslateMessage(pMsg);
}


// MyEditCtrl.h : header file
//
#if !defined(AFX_MYEDITCTRL_H__77B0F921_463A_11D2_BE76_0080ADB7F1BB__INCLUDED_)
#define AFX_MYEDITCTRL_H__77B0F921_463A_11D2_BE76_0080ADB7F1BB__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

/////////////////////////////////////////////////////////////////////////////
// CMyEditCtrl window

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

// Attributes
public:

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyEditCtrl)
public:
 virtual BOOL PreTranslateMessage(MSG* pMsg);
//}}AFX_VIRTUAL

// Implementation
public:
 virtual ~CMyEditCtrl();

// Generated message map functions
protected:
//{{AFX_MSG(CMyEditCtrl)
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before
previous line.

#endif // !defined(AFX_MYEDITCTRL_H__77B0F921_463A_11D2_BE76_0080ADB7F1BB__INCLUDED_)

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read