CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2006
    Posts
    22

    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

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: Cursor Position in Edit Control

    Do you want to set cursor or caret?

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    Oct 2006
    Posts
    22

    Re: Cursor Position in Edit Control

    Sorry i have mentioned wrongly, i need to set the caret position.

  4. #4
    Join Date
    Oct 2006
    Posts
    22

    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

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

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

  6. #6
    Join Date
    Oct 2006
    Posts
    22

    Re: Cursor Position in Edit Control

    Thanks for your reply now im able to do what i expected.

  7. #7
    Join Date
    May 2005
    Posts
    4,954

    Re: Cursor Position in Edit Control

    Quote 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
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  8. #8
    Join Date
    Oct 2006
    Posts
    22

    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

  9. #9
    Join Date
    Nov 2001
    Posts
    17

    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.

  10. #10
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Cursor Position in Edit Control

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured