-
November 28th, 2006, 11:42 PM
#1
Cursor Position in Edit Control
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
-
November 29th, 2006, 03:09 AM
#2
Re: Cursor Position in Edit Control
Do you want to set cursor or caret?
Cheers
-
November 29th, 2006, 05:36 AM
#3
Re: Cursor Position in Edit Control
Sorry i have mentioned wrongly, i need to set the caret position.
-
November 29th, 2006, 07:44 AM
#4
Re: Cursor Position in Edit Control
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
-
November 29th, 2006, 07:58 AM
#5
Re: Cursor Position in Edit Control
Send EM_SETSEL message or use Edit_SetSel macro defined in windowsx.h.
Code:
SendMessage(hWndEdit, EM_SETSEL, (WPARAM)3, (LPARAM)3);
SetFocus(hWndEdit);
or
Code:
#include <windowsx.h>
// ...
Edit_SetSel(hWndEdit, 3, 3);
SetFocus(hWndEdit);
-
November 29th, 2006, 11:38 PM
#6
Re: Cursor Position in Edit Control
Thanks for your reply now im able to do what i expected.
-
November 30th, 2006, 04:22 AM
#7
Re: Cursor Position in Edit Control
Originally Posted by sasi_kumar_irtt
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
Cheers
-
November 30th, 2006, 07:31 AM
#8
Re: Cursor Position in Edit Control
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
-
January 17th, 2007, 10:54 AM
#9
Re: Cursor Position in Edit Control
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.
-
January 18th, 2007, 12:07 AM
#10
Re: Cursor Position in Edit Control
Originally Posted by sasi_kumar_irtt
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/de...itcontrols.asp
MSDN should always be the first stop for anything microsoft related.
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
|