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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • QA teams don't have time to test everything yet they can't afford to ship buggy code. Learn how Coverity can help organizations shrink their testing cycles and reduce regression risk by focusing their manual and automated testing based on the impact of change.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds