CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    87

    Setting cursor in a dialog's edit-ctrl

    I want to set the cursor into an edit-ctrl preferably to a specific position. As far as I understood I have to set the focus to this ctrl and then doing a selection in that box. As SetFocus() is said not to work in dialogs properly (see http://blog.m-ri.de/index.php/2007/0...wm_nextdlgctl/), I used WM_NEXTDLGCTL to achieve that. EM_SETSEL with startpos equal to endpos shall provide positioning. Does anyone sees an error in the following which does not work as expected?
    Code:
                    SendMessage(WM_NEXTDLGCTL, WPARAM(GetDlgItem(Item)->m_hWnd), LPARAM(LOWORD(TRUE)));
                    GetDlgItem(Item)->SendMessage(EM_SETSEL, WPARAM(INT(VarValue.length())), LPARAM(INT(VarValue.length())));
    BTW: Item is the ID of the ctrl. VarValue the string being on display in the ctrl.

    Many Thanks in advance for any reply!

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Setting cursor in a dialog's edit-ctrl

    Quote Originally Posted by greve View Post
    ...which does not work as expected?
    Well, how does it work?
    Also, LOWORD(TRUE) makes no sence. You should use MAKELPARAM macro.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

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

    Re: Setting cursor in a dialog's edit-ctrl

    Beside that VladimirF already said.

    First, you don't reallay need here WM_NEXTDLGCTL instead of SetFocus, because you don't need additional dialog box management operations performed by WM_NEXTDLGCTL (update the default button border or select the edit text).
    Second, it has no sense to keep the edit text lenght in a member variable as long as you can get it anytime with a GetWindowTextLength call.
    An finally, once you are using MFC, it's easier to use CEdit class methods insted of directly sending messages.

    So, you can write something like this:
    Code:
       CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT2);
       const int nLength = pEdit->GetWindowTextLength();
       pEdit->SetSel(nLength, nLength);
       pEdit->SetFocus();
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    87

    Re: Setting cursor in a dialog's edit-ctrl

    After having done more detailed debugging I've to commit that the posted problem was described correctly in terms of the phenomenon (cursor not set correctly) but my first attempt to solve it was wrong: The cursor is set correctly, but immediately after having been set it is removed again.

    The edit-ctrl is handled (setting content and position of the cursor) within a handler of a CTreeCtrl (OnNotify(TVN_SELCHANGED, ...). Although the handling of the edit-ctrl is at the very end of the said handler it looks like as if the marking of the selected item in the CTreeCtrl-object happens after the handling of the edit-ctrl. Does anybody know whether the focus being set to the edit-ctrl is then passed to the tree-ctrl? Are there any ideas to bypass this?

    Thanks agin for comments!

  5. #5
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Setting cursor in a dialog's edit-ctrl

    try to use

    Code:
       pEdit->SetFocus();
    or

    according to your method..

    Code:
     SendMessage(WM_NEXTDLGCTL, WPARAM(GetDlgItem(Item)->m_hWnd), LPARAM(LOWORD(TRUE)));


    at the end of the handler. of a CTreeCtrl .. so that the focus remains at the edit box at the end.

  6. #6
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    87

    Re: Setting cursor in a dialog's edit-ctrl

    @vcdebugger: As said in the previous post:
    Although the handling of the edit-ctrl is at the very end of the said handler ...

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