Click to See Complete Forum and Search --> : Cursor Position in Edit Control


sasi_kumar_irtt
November 28th, 2006, 11:42 PM
I want to set my cursor position in a edit control in a specified place
say at the 9th character or 3rd character of the text displayed in the edit control.
When i use setfocus the cursor is placed at the first character of edit control.
Please help me how can i set the cursor in the specified position

golanshahar
November 29th, 2006, 03:09 AM
Do you want to set cursor or caret?

Cheers

sasi_kumar_irtt
November 29th, 2006, 05:36 AM
Sorry i have mentioned wrongly, i need to set the caret position.

sasi_kumar_irtt
November 29th, 2006, 07:44 AM
I have used the SetCaretPos funtion to set the caret position, the function also return 1 which means success, but im not getting the current position.

What i have diid is
I created a Edit control for a designing a calculator .
Created buttons for the same and written a function to set the value in the edit control when the buttons are pressed.
I obtain the current text in the edit control and append it with the value entered and set the new value in the edit control.
And call the setfocus function to set the focus to the edit window.

Then i call the SetCaretPos() according to the number of characters present in the edit control.
what i have to do to get the caret position at the desired place, please guide me

Tx
Sasi

ovidiucucu
November 29th, 2006, 07:58 AM
Send EM_SETSEL message or use Edit_SetSel macro defined in windowsx.h.
SendMessage(hWndEdit, EM_SETSEL, (WPARAM)3, (LPARAM)3);
SetFocus(hWndEdit);
or
#include <windowsx.h>
// ...
Edit_SetSel(hWndEdit, 3, 3);
SetFocus(hWndEdit);

sasi_kumar_irtt
November 29th, 2006, 11:38 PM
Thanks for your reply now im able to do what i expected.

golanshahar
November 30th, 2006, 04:22 AM
I have used the SetCaretPos funtion to set the caret position, the function also return 1 which means success, but im not getting the current position.

What i have diid is
I created a Edit control for a designing a calculator .
Created buttons for the same and written a function to set the value in the edit control when the buttons are pressed.
I obtain the current text in the edit control and append it with the value entered and set the new value in the edit control.
And call the setfocus function to set the focus to the edit window.

Then i call the SetCaretPos() according to the number of characters present in the edit control.
what i have to do to get the caret position at the desired place, please guide me

Tx
Sasi

I know your problem is solved but for general knowledge: SetCaretPos() Is Not Appropriate with CEdit or CRichEditCtrl Controls (http://support.microsoft.com/kb/259949) :)

Cheers

sasi_kumar_irtt
November 30th, 2006, 07:31 AM
Once again i need some help.
Using the EM_SETSEL i set the caret at the end of the string in the edit.
For one user button i used to remove the last charater of the string and display the rest.(since the caret is placed at the end of the string)

now when i place the cursor at the middle of the string in the edit control, i need to delete the character which is before the current caret position, how could u find the exact position of the caret and identify the previous character.

Sasi

Andonic
January 17th, 2007, 10:54 AM
Maybe this helps:

int pos1,pos2;
m_editControl->GetSel(pos1,pos2); /// this returns the position of the caret in characters
// After this you can get the string with GetWindowText
// i used it to simulate Ctrl+Enter when Enter only was pressed.
CString t;
m_editControl->GetWindowText(t);
t.Insert(pos1,'\r');
t.Insert(pos1+1,'\n');
m_editControl->SetWindowText(t);
m_editControl->SetSel( pos1+2,pos1+2,FALSE); // set the caret after the two new characters \r and \n

I hope this helps.

(sorry for my english)

bye.

kirants
January 18th, 2007, 12:07 AM
how could u find the exact position of the caret and identify the previous character.

Most answers can be found right here, if only you spend some time trying to find it.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/editcontrols/editcontrols.asp

MSDN should always be the first stop for anything microsoft related.