CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2009
    Posts
    103

    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....

  2. #2
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    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

  3. #3
    Join Date
    May 2009
    Posts
    103

    Resolved 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?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    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

  5. #5
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    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

  6. #6
    Join Date
    May 2009
    Posts
    103

    Re: RichEdit and SetSel when tabbing

    THANKS!!!

    That worked!! Got a love it when things don't work as they are SUPPOSE too!!

  7. #7
    Join Date
    May 2009
    Posts
    103

    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?

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  9. #9
    Join Date
    May 2009
    Posts
    103

    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
  •  





Click Here to Expand Forum to Full Width

Featured