Allowing the TAB key in Edit Controls | CodeGuru

Allowing the TAB key in Edit Controls

Introduction I was quite frustrated in with my application. I have a dialog with a simple multi-line edit (MLE) control. When I attempted to press the TAB key, the entire contents of my edit control would be highlighted! Evidently, this is the default action of an edit control when the Tab key is pressed. Anyway, […]

Written By
CodeGuru Staff
CodeGuru Staff
Nov 2, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Introduction

I was quite frustrated in with my application. I have a dialog with a simple multi-line edit (MLE) control.
When I attempted to press the TAB key, the entire contents of my edit control would be highlighted!
Evidently, this is the default action of an edit control when the Tab key is pressed. Anyway, I wanted the
Tab key to work as I would expect it to in a control that is supposedly mimicing an editor so I set out to
find a solution.

Solution

Basically, it came down to control’s the tried and true, PreTranslateMessage member function.
It works like this. PreTranslateMessage is a virtual member function that is called in order to give your control a chance
to do something before (or instead of) the default behavior of the control. Therefore, in this function,
I check to see if the message being sent is a WM_KEYDOWN message (indicating that the user has pressed some key).
I then determine if the key being pressed is the Tab key (whose id is VK_TAB). If these two cases are true,
I simply deselect any selected text and insert a tab character into the control (\t).

Implementing this Solution

Here are the steps necessary to implement my work-around.

  1. To your override of CDialog class, add the following method:
  2. virtual BOOL PreTranslateMessage(MSG *);
    Implement this function, for example:
    BOOL CEditExDlg::PreTranslateMessage(MSG* pMsg)
    {
     if ((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_TAB))
     {
      // get the char index of the caret position
      int nPos = LOWORD(m_MyEditCtl.CharFromPos(m_MyEditCtl.GetCaretPos()));
      // select zero chars
      m_MyEditCtl.SetSel(nPos, nPos);
      // then replace that selection with a TAB
      m_MyEditCtl.ReplaceSel("\t", TRUE);
      // no need to do a msg translation, so quit. 
      // that way no further processing gets done
      return TRUE;
     }
     //just let other massages to work normally
     return CDialog::PreTranslateMessage(pMsg);
    }
    
    
Advertisement

Downloads

Download demo project - 10 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.