TextBoxBase.ScrollToCaret 方法

定義

將控制項內容捲動到目前的插入點位置。

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 將控制內容滾動,直到滑槽在控制區底部可見。 若滑槽位於控制區可見區域之上,此方法會滾動控制區內容,直到滑槽在控制區頂部可見。 你可以在多行文字框中使用此方法,確保目前的文字入口位於控制項可見區域內。

Note

若對照組未對焦或裝槽已位於對照組可見區域,此方法無效。

適用於