none
Cursor position in textbox RRS feed

  • Question

  • Hi,  I have applied many textboxes and always the cursor was positioned at the end of the text (already in the textbox). Semingly by default.

    In an added small form with two textboxes, in one the cursor is positioned at the front of the text, in the second textbox at the end. I have not applied the Textbox.Select() method. I assume their is a default setting in VS2013 (Update5), but couldn't find it. Applying the Select method is an option, but I like to understand where the behaviour comes from.

    Any suggestion is highly appreciated.

    Friday, September 9, 2016 10:57 AM

Answers

  • Thanks Viorel for your reply.

    I used the mouse click to get the focus on the textbox concerned.The existing text was colored, so the focus was given, but (in my case a small vertical line as always) is shown, but now in front of the existing text instead of at the back (right of the text end).

    Instead of applying the Select method, I just deleted the textbox and added a new one (accepting the burden of repairing some parts of the code (which is much larger than the case of two textboxes).

    To my surprise the problem was gone. So it seems the instantiation of the textbox was damaged?

    Never heard or read this can happen.

    All actions where executed inside VS2013.

    • Marked as answer by GerritV2 Monday, September 12, 2016 2:52 PM
    Friday, September 9, 2016 8:14 PM
  • Hi GerritV2,

    Thank you for posting here.

    I am not sure that what type of your project. It is winform project or wpf project? 

    Since your issue is related to control design. The MSDN forum has special forum for control design.

    I can help you move thread to here.

    >>Applying the Select method is an option, but I like to understand where the behaviour comes from.

    If you want to know how to set cursor position.  I found a similar solution.

    // save current cursor position and selection 
            int start = textBox.SelectionStart;
            int length = textBox.SelectionLength;
    
            Point point = new Point();
            User32.GetCaretPos(out point);
    
            // update text
            textBox.Text = value;
    
            // restore cursor position and selection
            textBox.Select(start, length);
            User32.SetCaretPos(point.X, point.Y);

    If your issue has been resolved, please remember to close your thread by marking useful posts as answer.

    Best Regards,

    Hart


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.


    • Edited by Hart Wang Monday, September 12, 2016 6:15 AM
    • Marked as answer by GerritV2 Monday, September 12, 2016 2:52 PM
    Monday, September 12, 2016 6:09 AM

All replies

  • How did you determine the current caret position, taking into consideration that the caret is visible for the focused textbox only, and setting the focus with mouse or keyboard usually affects the selection?

    Friday, September 9, 2016 7:34 PM
  • Thanks Viorel for your reply.

    I used the mouse click to get the focus on the textbox concerned.The existing text was colored, so the focus was given, but (in my case a small vertical line as always) is shown, but now in front of the existing text instead of at the back (right of the text end).

    Instead of applying the Select method, I just deleted the textbox and added a new one (accepting the burden of repairing some parts of the code (which is much larger than the case of two textboxes).

    To my surprise the problem was gone. So it seems the instantiation of the textbox was damaged?

    Never heard or read this can happen.

    All actions where executed inside VS2013.

    • Marked as answer by GerritV2 Monday, September 12, 2016 2:52 PM
    Friday, September 9, 2016 8:14 PM
  • Hi GerritV2,

    Thank you for posting here.

    I am not sure that what type of your project. It is winform project or wpf project? 

    Since your issue is related to control design. The MSDN forum has special forum for control design.

    I can help you move thread to here.

    >>Applying the Select method is an option, but I like to understand where the behaviour comes from.

    If you want to know how to set cursor position.  I found a similar solution.

    // save current cursor position and selection 
            int start = textBox.SelectionStart;
            int length = textBox.SelectionLength;
    
            Point point = new Point();
            User32.GetCaretPos(out point);
    
            // update text
            textBox.Text = value;
    
            // restore cursor position and selection
            textBox.Select(start, length);
            User32.SetCaretPos(point.X, point.Y);

    If your issue has been resolved, please remember to close your thread by marking useful posts as answer.

    Best Regards,

    Hart


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.


    • Edited by Hart Wang Monday, September 12, 2016 6:15 AM
    • Marked as answer by GerritV2 Monday, September 12, 2016 2:52 PM
    Monday, September 12, 2016 6:09 AM
  • Hi Hart Cheng,

    Thanks for your reply and suggestion, as the problem disappeared after removing the textbox concerned and adding a new textbox instead, I will try your suggestion and keep it in reserve. For this many thanks. As menthioned in my previous post, it seems the textbox was somehow damaged, or a setting was unwillingly changed. For now its ok: the program is performing again, but I don't like  sudden changes I don't understand or has not applied on purpose.

    Regards,

    GerritV2

    Monday, September 12, 2016 3:01 PM