KeyDown 事件、TextBox 控件、CurLine、CurTargetX、CurX 和 Text 属性示例KeyDown event, TextBox control, CurLine, CurTargetX, CurX, Text properties example

下面的示例跟踪多行**TextBox** 中的**CurLineCurTargetXCurX** 属性设置。The following example tracks the CurLine, CurTargetX, and CurX property settings in a multiline TextBox. 当用户在**Text** 属性中键入时, 这些设置会在**KeyUp** 事件中发生变化, 移动插入点, 并使用键盘扩展选定内容。These settings change in the KeyUp event as the user types into the Text property, moves the insertion point, and extends the selection by using the keyboard.

若要使用此示例,请按照下列步骤操作:To use this example, follow these steps:

  1. 将此示例代码复制到窗体的声明部分。Copy this sample code to the Declarations portion of a form.

  2. 向窗体中添加一个名为 TextBox1 的大型 TextBoxAdd one large TextBox named TextBox1 to the form.

  3. 在一个列中添加三个 TextBox 控件,分别名为 TextBox2、TextBox3 和 TextBox4。Add three TextBox controls named TextBox2, TextBox3, and TextBox4 in a column.

Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) 
 TextBox2.Text = TextBox1.CurLine 
 TextBox3.Text = TextBox1.CurX 
 TextBox4.Text = TextBox1.CurTargetX 
End Sub

Private Sub UserForm_Initialize() 
 TextBox1.MultiLine = True 
 
 TextBox1.Text = "Type your text here. User CTRL + ENTER to start a new line." 
End Sub

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.