Allowing the TAB key in Edit Controls
Posted
by Hilmi Jauffer
on November 2nd, 2000
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.- To your override of CDialog class, add the following method:
- Implement this function, for example:
virtual BOOL PreTranslateMessage(MSG *);
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);
}

Comments
Goal
Posted by snareenactina on 12/09/2012 09:36amararmenian You may not be able to find this page because of: Data from over 100 sources as well as proprietary data on key industries. All organized, translated and updated weekly covering city and provincial economies and 10 of China's leading industries. Over the past few years, Vietnam has begun to rise up in the rankings of world countries. Here is a quick rundown of how it compares to other countries: exact Gut instinct and human nature still rule the day. hlavnÃho G. Recent Revival goalc Schools are supposed to be sanctuaries of education, and the threat of being sent to prison for a minor infraction likely discourages attendance for some at-risk youth. restrooms These data show the tax burden (personal and corporate) and national debt as a percentage of GDP. Samples are taken at 10 year intervals (snapshots, but the rolling averages are very close). compound Just so you know whom your readership comprises of, I live in Ireland and have a keen interest in eschatology. I chanced upon the two sites and have now followed your posts now for a number of weeks. Hugely informative and well composed. Thank you. merida One dimension of growth rebalancing on which there has in fact been progress is related to Chinaââ¬â¢s external balances. The trade surplus fell from its peak level of over 7 percent of GDP in 2007 to 3 percent in 2010 (Figure 8). Export and import growth took a tumble during the financial crisis but have since recovered sharply (Figure 9). The investment boom has driven import growth sharply higher while export growth has been strong but lower than import growth due to weaknesses in advanced country markets that absorb a large fraction of Chinaââ¬â¢s imports. strictly Macroeconomics is a broad topic, which scholars and students find complex to analyze. This is because this topic focuses on how economic forces affects political, social, economic, and labor institutions
ReplyCan you control the width of the Tab space?
Posted by mtb2440 on 10/28/2005 05:14pmI would like to set it to 4 spaces. Thanks - great technique. MB
ReplyON_WM_GETDLGCODE
Posted by Legacy on 10/28/2003 12:00amOriginally posted by: Andy Walker
ReplyThanks for the post
Posted by Legacy on 04/22/2003 12:00amOriginally posted by: Newbie
Thanks for the post. I don't due alot of windows programming but ran into this same problem and because of your post I made this change in under 5 minutes. So thanks for taking the time to post.
ReplyHow do I catch any key without using MFC?
Posted by Legacy on 03/08/2003 12:00amOriginally posted by: Witek
If keybord focus is in some edit control and I want to catch arrow keys without using MFC. Can anybody helps?
ReplyGreate Thank you..
Posted by Legacy on 02/19/2003 12:00amOriginally posted by: Kim Su-Jung
ReplyCan we display new line chracter as well
Posted by Legacy on 08/06/2002 12:00amOriginally posted by: Ramesh
I have an Edit Control in VC++, and I basically use it for logging. So I assign the member variable to the log info I want and then I use the UpdateData(FALSE).
ReplyNow the problem I have is I want the new line character to function as new line, but instead it displays some special character there. How do I send a new line character to my Edit Control.
We can also use CEdit::PreTranslateMessage
Posted by Legacy on 01/02/2002 12:00amOriginally posted by: Franz Wong
Replya simple question???
Posted by Legacy on 12/07/2001 12:00amOriginally posted by: John Miller
Hi,
This question may look stupid or simple for you folks here,
but I am still puzzled with this:
is a control (such as a Edit box or Static control) area
still the client area of the main dialog window?
When the mouse move occurs within any control box area,
it looks that the neither the WM_MOUSEMOVE nor the WM_NCMOUSEMOVE message does not get sent!
When you put a control on top of the dialog client area,
how do you capture the mouse move event WITHIN the control
area????
Hope some one would kindly point it out, thanks!
Reply
if this isn't the only control in the window ...
Posted by Legacy on 08/15/2001 12:00amOriginally posted by: Rick Phillips
ReplyLoading, Please Wait ...