.NET Framework Class Library
TextBoxBase..::.ScrollToCaret Method

Scrolls the contents of the control to the current caret position.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Syntax

Visual Basic (Declaration)
Public Sub ScrollToCaret
Visual Basic (Usage)
Dim instance As TextBoxBase

instance.ScrollToCaret()
C#
public void ScrollToCaret()
Visual C++
public:
void ScrollToCaret()
JScript
public function ScrollToCaret()
Remarks

This method enables you to scroll the contents of the control until the caret is within the visible region of the control. If the caret is positioned below the visible region of the control, the ScrollToCaret method will scroll the contents of the control until the caret is visible at the bottom of the control. If the caret is positioned above the visible region of the control, this method scrolls the contents of the control until the caret is visible at the top of the control. You can use this method in a multiline text box to ensure that the current text entry point is within the visible region of the control.

NoteNote:

This method has no effect if the control does not have focus or if the caret is already positioned in the visible region of the control.

Examples

The following code example demonstrates how use the Keys enumeration and the ScrollToCaret method. To run the example, paste the following code in a form containing a TextBox control called TextBox1 and a RichTextBox control called RichTextBox1. This example requires that the event-handling method has been associated with the KeyDown event.

Visual Basic
'Handles the Enter key being pressed while TextBox1 has focus. 
Private Sub TextBox1_KeyDown(ByVal sender As Object, _
    ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
    TextBox1.HideSelection = False
    If e.KeyCode = Keys.Enter Then
        e.Handled = True

        ' Copy the text from TextBox1 to RichTextBox1, add a CRLF after 
        ' the copied text, and keep the caret in view.
        RichTextBox1.SelectedText = TextBox1.Text + _
            Microsoft.VisualBasic.vbCrLf
        RichTextBox1.ScrollToCaret()
    End If
End Sub
C#
    //Handles the Enter key being pressed while TextBox1 has focus. 
    private void TextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        TextBox1.HideSelection = false;
        if (e.KeyCode==Keys.Enter)
        {
            e.Handled = true;

            // Copy the text from TextBox1 to RichTextBox1, add a CRLF after 
            // the copied text, and keep the caret in view.
            RichTextBox1.SelectedText = TextBox1.Text + "\r\n";
            RichTextBox1.ScrollToCaret();
        }
    }
Visual C++
private:
   //Handles the Enter key being pressed while TextBox1 has focus. 
   void TextBox1_KeyDown( Object^ /*sender*/, KeyEventArgs^ e )
   {
      TextBox1->HideSelection = false;
      if ( e->KeyCode == Keys::Enter )
      {
         e->Handled = true;

         // Copy the text from TextBox1 to RichTextBox1, add a CRLF after 
         // the copied text, and keep the caret in view.
         RichTextBox1->SelectedText = String::Concat( TextBox1->Text, "\r\n" );
         RichTextBox1->ScrollToCaret();
      }
   }
Metadata
The following code example demonstrates how use the Keys enumeration and the TextBoxBase.ScrollToCaret method. To run the example, paste the following code
in a form containing a TextBox control called TextBox1 and a RichTextBox control called RichTextBox1. This example assumes the event-handling method has been associated with the KeyDown event.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
See Also

Reference

Tags :


Page view tracker