Pressing Tab moves cursor rather than moving to next text box
Hi, there's probably some property value here that I'm missing or something, but I can't seem to figure it out. I'm creating a simple form with some small fields such as Name, Address, etc. Most are single-lined, but the address text box is multi-line (not sure if this matters).
The problem is I want the user to be able to tab through the individual edit controls, but when I press tab, it either beeps at me or in the case of the multiline edit control, moves the cursor over like I was in a text editor. Each of these items has the TabStop property equal to true.
What can I do to make the tab key actually tab between items rather than move the cursor in the text box?
Thanks
# 1 Re: Pressing Tab moves cursor rather than moving to next text box
Are you using MFC? I presume, yes.
Derive from CEdit, handle WM_GETDLGCODE and add DLGC_WANTTAB to the return code.
UINT CMyEdit::OnGetDlgCode()
{
UINT nCode = CEdit::OnGetDlgCode();
return DLGC_WANTTAB | nCode;
}
Now, the TAB key will be handled inside the edit control.
So next, in the WM_KEYDOWN message handler move the caret on TAB keystroke.
void CMyEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(VK_TAB == nChar)
{
int nStartChar, nEndChar;
int nTextLength = GetWindowTextLength();
GetSel(nStartChar, nEndChar);
SHORT nKeyState = ::GetKeyState(VK_SHIFT);
int nNewSel;
if(0x80 & nKeyState) // <Shift> pressed - move caret to left
nNewSel = max(0, (nStartChar - 1));
else // - move caret to right
nNewSel = min((nEndChar + 1), nTextLength);
SetSel(nNewSel, nNewSel);
}
else
{
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
}
Note: The above example is for single-line edit control. For a multi-line it can be a little bit different, but I'm sure you can handle it... ;)
# 2 Re: Pressing Tab moves cursor rather than moving to next text box
Thanks for the response. Sorry, I should have mentioned that I wasn't using MFC (my boss was anti-MFC and all things object-oriented); however, what I did get from this is it sounds like there are no simple ways to do this - I actually have to trap the Tab key and then handle it. Does that seem ridiculous to anyone else other than me? I mean, isn't that a basic functionality most text boxes would have? Why have a Tabstop value if you can't really tab from it. My goodness, Microsoft never ceases to amaze me.
If anyone knows how to do it without MFC, I would greatly appreciate a few pointers. I know I could always hook it, but I was hoping not to do that for something so simple.
Thanks.
# 3 Re: Pressing Tab moves cursor rather than moving to next text box
Why have a Tabstop value if you can't really tab from it. Well. You got it wrong. Having WS_TABSTOP style for myself means, it is ok to stop at me when tab/or shift tab has been done from prev/next control in the zorder. It is for controlling the tabbing into than the tabbing from. So, you seem to have thought of it exactly the opposite of what it is supposed to be.
If anyone knows how to do it without MFC, I would greatly appreciate a few pointers. I know I could always hook it, but I was hoping not to do that for something so simple.
Wrong forum. Non-MFC questions go to C++ and WinAPI forum. Anyways, the non-MFC way to do it is to subclass the edit control and handle the same messages that ovidiucucu mentioned.
# 5 Re: Pressing Tab moves cursor rather than moving to next text box
Actually, subclassing was the hard and unneccesary way! I found the solution. Setting the property to tabstop was actually sufficient. The real problem was that I needed to change my main message loop so that it called IsDialogMessage() and continued to the next message before it tried to translate or dispatch the message. After having the message correctly handled by that function, the tabstop property worked great!