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_)



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • This paper introduces IBM Java on the IBM PowerLinux 7R2 server and describes IBM's implementation of the Java platform, which includes IBM's Java Virtual Machine and development toolkit.

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds