none
Moving Caret in textbox RRS feed

  • Question

  • How can I move the caret to the first visible line in the textbox control, having perhaps scrolled many lines up and or may have clicked on the text somewhere in the textbox.


    Wednesday, April 4, 2018 11:10 AM

Answers

  • Seems that you can use a special message:

     

    const int EM_GETFIRSTVISIBLELINE = 0x00CE;

     

    [DllImport( "User32" )]

    static extern int SendMessage( IntPtr hwnd, int msg, int wParam, int lParam );

     

    . . .

    int i = SendMessage( textBox1.Handle, EM_GETFIRSTVISIBLELINE, 0, 0 );

    textBox1.Focus();

    textBox1.SelectionLength = 0;

    textBox1.SelectionStart = textBox1.GetFirstCharIndexFromLine( i );

     


    • Edited by Viorel_MVP Wednesday, April 4, 2018 12:55 PM
    • Marked as answer by JonGraef Wednesday, April 4, 2018 1:34 PM
    Wednesday, April 4, 2018 12:54 PM

All replies

  • I have seen this page you mention and it does not answer my question.

    The key part of my question is "first visible", in a multi line text box that has been scrolled.

    • Edited by JonGraef Wednesday, April 4, 2018 11:45 AM
    Wednesday, April 4, 2018 11:44 AM
  • Seems that you can use a special message:

     

    const int EM_GETFIRSTVISIBLELINE = 0x00CE;

     

    [DllImport( "User32" )]

    static extern int SendMessage( IntPtr hwnd, int msg, int wParam, int lParam );

     

    . . .

    int i = SendMessage( textBox1.Handle, EM_GETFIRSTVISIBLELINE, 0, 0 );

    textBox1.Focus();

    textBox1.SelectionLength = 0;

    textBox1.SelectionStart = textBox1.GetFirstCharIndexFromLine( i );

     


    • Edited by Viorel_MVP Wednesday, April 4, 2018 12:55 PM
    • Marked as answer by JonGraef Wednesday, April 4, 2018 1:34 PM
    Wednesday, April 4, 2018 12:54 PM
  • Thanks for the reply.

    Where do I put this part of the code

    const int EM_GETFIRSTVISIBLELINE = 0x00CE;
    
     
    
    [DllImport( "User32" )]
    
    static extern int SendMessage( IntPtr hwnd, int msg, int wParam, int lParam );

    I tried to put it under the using declarations but get error

    Wednesday, April 4, 2018 1:04 PM
  • I think I see where to put it

    https://social.msdn.microsoft.com/Forums/SqlServer/en-US/d3fdaf8f-ff46-4d6e-a549-80c0e050c4c7/getfirstcharindexfromposition-question?forum=winforms

    Wednesday, April 4, 2018 1:07 PM