-
August 15th, 2011, 09:50 AM
#1
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!
-
August 15th, 2011, 10:41 AM
#2
Re: Setting cursor in a dialog's edit-ctrl
Originally Posted by greve
...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...
-
August 16th, 2011, 07:02 AM
#3
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();
-
August 17th, 2011, 07:19 AM
#4
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!
-
August 17th, 2011, 07:30 AM
#5
Re: Setting cursor in a dialog's edit-ctrl
try to use
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.
-
August 17th, 2011, 09:33 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|