TextBoxBase.ScrollToCaret 方法

定義

捲動控制項的內容至目前插入號 (Caret) 所在位置。Scrolls the contents of the control to the current caret position.

public:
 void ScrollToCaret();
public void ScrollToCaret ();
member this.ScrollToCaret : unit -> unit
Public Sub ScrollToCaret ()

範例

下列程式碼範例示範如何使用 Keys 列舉和 ScrollToCaret 方法,以確保在按下 ENTER 鍵之後,螢幕上一律會顯示插入點所表示的文字插入點。The following code example demonstrates how to use the Keys enumeration and the ScrollToCaret method to ensure that the text insertion point, represented by the caret, is always visible on the screen after the ENTER key has been pressed. 若要執行範例,請在包含控制項的表單中貼上下列程式碼, TextBox TextBox1RichTextBox 呼叫名為的控制項 RichTextBox1To run the example, paste the following code in a form containing a TextBox control called TextBox1 and a RichTextBox control called RichTextBox1. 這個範例會要求事件處理方法已經與事件相關聯 KeyDownThis example requires that the event-handling method has been associated with the KeyDown event.

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();
      }
   }
//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();
    }
}
'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

備註

這個方法可讓您滾動控制項的內容,直到插入點位於控制項的可見區域內。This method enables you to scroll the contents of the control until the caret is within the visible region of the control. 如果插入號位於控制項的可見區域下方,此 ScrollToCaret 方法將會滾動控制項的內容,直到控制項底部顯示插入號為止。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.

注意

如果控制項沒有焦點,或如果插入號已定位於控制項的可見區域中,則這個方法不會有任何作用。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.

適用於