TextBoxBase.ScrollToCaret 方法

定義

捲動控制項的內容至目前插入號 (Caret) 所在位置。

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

範例

下列程式碼範例示範如何使用 Keys 列舉和 ScrollToCaret 方法,以確保插入號所代表的文字插入點在按下 ENTER 鍵之後,一律會顯示在螢幕上。 若要執行範例,請將下列程式碼貼到表單中,其中包含 TextBox 名為 的 TextBox1 控制項,以及 RichTextBox 名為 的 RichTextBox1 控制項。 此範例要求事件處理方法已與事件相關聯 KeyDown

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

備註

這個方法可讓您捲動控制項的內容,直到插入號位於控制項的可見區域內為止。 如果插入號位於控制項的可見區域下方,則方法會捲動控制項的內容, ScrollToCaret 直到控制項底部顯示插入號為止。 如果插入號位於控制項的可見區域上方,這個方法會捲動控制項的內容,直到插入號顯示在控制項頂端為止。 您可以在多行文字方塊中使用這個方法,以確保目前的文字進入點位於控制項的可見區域內。

注意

如果控制項沒有焦點,或插入號已經位於控制項的可見區域中,這個方法就沒有任何作用。

適用於