TextBoxBase.ScrollToCaret 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将控件的内容滚动到当前插入点位置。
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 将滚动控件的内容,直到插入符号在控件底部可见。 如果插入符号位于控件的可见区域上方,此方法将滚动控件的内容,直到插入符号在控件顶部可见。 可以在多行文本框中使用此方法,以确保当前文本入口点位于控件的可见区域中。
注释
如果控件没有焦点或插入点已定位在控件的可见区域中,则此方法不起作用。