Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a large Multiline CEdit in a Dialog based App in VC 5.0. How do I make it accept a Tab Key as an 'Edit'tab, rather than as a command to jump to the next control. The Rc Editor has an option for 'WantReturn' but not for 'Want Tab'
Posted

Given below is what I would call a 'hack' but it works ( you have to check for any undesirable side-affects :=). If you find a better/ ideal solution please post it here.

1. subclass the edit control

.h
==
class CPDEdit: public CEdit
{
public:
	DECLARE_MESSAGE_MAP()
	afx_msg LRESULT OnKeyDown(WPARAM, LPARAM);
	afx_msg LRESULT OnKeyUp(WPARAM, LPARAM);
};


.cpp
====
BEGIN_MESSAGE_MAP(CPDEdit, CEdit)
    ON_MESSAGE(WM_KEYDOWN, OnKeyDown)
    ON_MESSAGE(WM_KEYUP, OnKeyUp)
END_MESSAGE_MAP()



LRESULT CPDEdit::OnKeyDown(WPARAM w, LPARAM l) {
	if ('\t' == w) {
		return 0;
	}
	return DefWindowProc(WM_KEYDOWN, w, l);
}
LRESULT CPDEdit::OnKeyUp(WPARAM w, LPARAM l) {
	if ('\t' == w ) {
		DWORD s = GetSel();
		if (0 != s && LOWORD(s) == HIWORD(s)) {
			ReplaceSel(_T("\t"));
                        return 0;
		}
	}
        return DefWindowProc(WM_KEYUP, w, l);
}


2. Create a member variable for the Edit of control type from the dialog resource editor (right-click the control and select "Add variable ...".
3. Change the type of this variable from CEdit to CPDEdit manually in the dialog header.
 
Share this answer
 
v2
Comments
Dalek Dave 28-Oct-10 3:56am    
Comprehensive Answer.
C++
Given below is what I would call a 'hack


I would not consider this a hack! You derrived a New class from the CEdit Class, with documented and specified behaviour. This is what both CPP and MFC, and also the WndProc system intended. Does not mean that the best of intentions on all fronts won't go wrong though! Happy to say, works in my case.

Thanks,


Bram.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900