TextBoxBase.ScrollToCaret 方法

将控件的内容滚动到当前插入符号位置。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Sub ScrollToCaret
用法
Dim instance As TextBoxBase

instance.ScrollToCaret
public void ScrollToCaret ()
public:
void ScrollToCaret ()
public void ScrollToCaret ()
public function ScrollToCaret ()

备注

此方法使您能够一直滚动控件的内容,直到插入符号处于该控件的可见区域为止。如果插入符号位于该控件的可见区域之下,则 ScrollToCaret 方法将把控件的内容一直滚动到插入符号出现在控件的底部为止。如果插入符号位于该控件的可见区域之上,则此方法将把控件的内容一直滚动到插入符号出现在控件的顶部为止。可以在多行文本框中使用此方法,以确保当前文本的输入点位于该控件的可见区域内。

提示

如果控件没有焦点,或者插入符号已经位于该控件的可见区域内,则此方法无效。

示例

下面的代码示例阐释了如何使用 Keys 枚举和 ScrollToCaret 方法。若要运行此示例,请将以下代码粘贴到一个窗体中,该窗体包含一个名为 TextBox1TextBox 控件和一个名为 RichTextBox1RichTextBox 控件。该示例要求事件处理方法已经与 KeyDown 事件相关联。

'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
//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();
    }
}
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.set_HideSelection(false);
    if (e.get_KeyCode().Equals(Keys.Enter)) {
        e.set_Handled(true);
        // Copy the text from textBox1 to richTextBox1, add a CRLF after 
        // the copied text, and keep the caret in view.
        richTextBox1.set_SelectedText(textBox1.get_Text() + "\r\n");
        richTextBox1.ScrollToCaret();
    }
} //textBox1_KeyDown

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

TextBoxBase 类
TextBoxBase 成员
System.Windows.Forms 命名空间