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

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds