-
May 21st, 2012, 09:38 PM
#1
RichEdit and SetSel when tabbing
I have a dialog that has a RichEdit (called IDC_MESSAGE) in it. If there is something in the RichEdit and you tab into it, it selects everything. What I want to do is put the cursor at the beginning with nothing selected.
I know I need to use SetSel and I believe I need to set it when the RichEdit gets focus... but, can't figure out how to catch that. Looking in the ClassWizard, the only options I have for the RichEdit is the EN_SETFOCUS and NM_SETFOCUS. I tried setting both of those but neither one seems to fire when tabbing into that field.
Not sure where to go from here.
Any help is greatly appreciated....
-
May 22nd, 2012, 12:31 PM
#2
Re: RichEdit and SetSel when tabbing
Use SetSel(0,0) of CRichEdit Control in OnEnSetFocus function of CRichEdit Control.
Code:
void CMyDlg::OnEnSetfocusRicheditMessages()
{
// TODO: Add your control notification handler code here
m_RichEditCtrlSetSel(0,0);
}
I have this code working in my application. Also please make sure your have TabStop True in RichEdit control properties.
Last edited by Naumaan; May 22nd, 2012 at 12:36 PM.
A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.
NAUMAAN
-
May 22nd, 2012, 06:56 PM
#3
Re: RichEdit and SetSel when tabbing
Hmmm, that's what I thought, but it still doesn't work.....
Here's what I got... I used the ClassWizard and this is what was created....
Code:
BEGIN_MESSAGE_MAP(CMessageDlg, CDialog)
//{{AFX_MSG_MAP(CMessageDlg)
...
ON_NOTIFY(EN_SETFOCUS, IDC_MESSAGE, OnEnSetfocusMessage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CMessageDlg::OnEnSetfocusMessage(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
m_messageCtl.SetSel(0,0);
}
When I tab into the IDC_MESSAGE, everything still highlights. If I put a breakpoint on the SetSel line, it never fires.
Any suggestions?
-
May 23rd, 2012, 04:35 AM
#4
Re: RichEdit and SetSel when tabbing
According to MSDN
The EN_SETFOCUS notification message is sent when an edit control receives the keyboard focus. The parent window of the edit control receives this notification message through a WM_COMMAND message.
Although I have no idea why your "ClassWizard" put ON_NOTIFY macro rather then ON_COMMAND one, you could try to change it by hand.
Victor Nijegorodov
-
May 23rd, 2012, 10:31 AM
#5
Re: RichEdit and SetSel when tabbing
You can try this in your code.
Code:
BEGIN_MESSAGE_MAP(CMessageDlg, CDialog)
//{{AFX_MSG_MAP(CMessageDlg)
...
ON_EN_SETFOCUS(IDC_RICHEDIT_YOURCONTROL,&CMessageDlg::OnEnSetfocusMessage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CMessageDlg::OnEnSetfocusMessage()
{
// TODO: Add your control notification handler code here
m_messageCtl.SetSel(0,0);
}
A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.
NAUMAAN
-
May 23rd, 2012, 08:01 PM
#6
Re: RichEdit and SetSel when tabbing
THANKS!!!
That worked!! Got a love it when things don't work as they are SUPPOSE too!!
-
June 1st, 2012, 09:44 PM
#7
Re: RichEdit and SetSel when tabbing
WHOOPS!!! Thought it was working. Then it compiled it and ran the EXE and it crashes.
First off, I had to modify this line to get it to compile correctly:
Code:
ON_EN_SETFOCUS(IDC_RICHEDIT_YOURCONTROL,&CMessageDlg::OnEnSetfocusMessage)
to
Code:
ON_EN_SETFOCUS(IDC_RICHEDIT_YOURCONTROL,OnEnSetfocusMessage)
I got a compile error (actually 6) because of this one line....
Code:
C:\myapp\MessageDlg.cpp(71) : error C2059: syntax error : '&&'
C:\myapp\MessageDlg.cpp(71) : error C2143: syntax error : missing ';' before '}'
C:\myapp\MessageDlg.cpp(71) : error C2143: syntax error : missing ';' before '}'
C:\myapp\MessageDlg.cpp(74) : error C2143: syntax error : missing ';' before '{'
C:\myapp\MessageDlg.cpp(74) : error C2447: missing function header (old-style formal list?)
C:\myapp\MessageDlg.cpp(74) : error C2143: syntax error : missing ';' before '}'
The modified one worked when stepping and debugging, but when compiled the whole app crashed. If I take this out, it no longer crashes (but of course doesn't work).
Any suggestions?
-
June 2nd, 2012, 02:53 AM
#8
Re: RichEdit and SetSel when tabbing
Indeed, for rich-edit control, VS6 Class Wizard shows both EN_SETFOCUS and NM_SETFOCUS in the list, although Rich-edit 1.0 which can be taken from VS6 toolbox, sends only EN_SETFOCUS (at least according to the documentation).
Moreover, it maps both notifications using ON_NOTIFY macro, which is not correct.
Let's take it as a little bug. If you still are using old VS6.0, then map manually the rich-edit control notifications. That's it.
Beware of the message handlers prototype! VS6 performs C-style cast when uses function pointers involved in message mapping. If you manually provide a wrong prototype, let's say void OnSetfocusSomeCtrl(NMHDR* pNMHDR, LRESULT* pResult) the program compiles but may crash at run-time (it crashes for sure in release build). Newer versions of Visual Studio resolved this issue by making static_cast.
Putting all together, including the advices of my colleagues, the following sample must compile, run and work with no problems:
Code:
class CMyDialog : public CDialog
{
// ...
CRichEditCtrl m_richeditTest;
// ...
// ...
afx_msg void OnSetfocusRichEditTest();
DECLARE_MESSAGE_MAP()
};
Code:
void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
// ...
// ...
DDX_Control(pDX, IDC_RICHEDIT_TEST, m_richeditTest);
//}}AFX_DATA_MAP
}
// ...
// ...
ON_EN_SETFOCUS(IDC_RICHEDIT_TEST, OnSetfocusRichEditTest)
END_MESSAGE_MAP()
void CMyDialog::OnSetfocusRichEditTest()
{
m_richeditTest.SetSel(0, 0);
}
Of course, must not forget to call AfxInitRichEdit in InitInstance of aplication (derived from CWinApp) class.
Last edited by ovidiucucu; June 3rd, 2012 at 01:04 AM.
-
June 2nd, 2012, 02:03 PM
#9
Re: RichEdit and SetSel when tabbing
Thank you, that fixed the bug.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|