KeyPress 事件示例KeyPress event example

以下示例使用**KeyPress** 事件将键击从一个**textbox** 复制到第二个textboxThe following example uses the KeyPress event to copy keystrokes from one TextBox to a second TextBox. 用户可键入适当标记的 TextBoxThe user types into the appropriately marked TextBox.

若要使用此示例,请将此示例代码复制到窗体的声明部分。To use this example, copy this sample code to the Declarations portion of a form. 确保该窗体包含:Make sure that the form contains:

  • 两个 TextBox 控件,分别名为 TextBox1 和 TextBox2。Two TextBox controls named TextBox1 and TextBox2.
Private Sub TextBox1_KeyPress(ByVal KeyAscii As _ 
 MSForms.ReturnInteger) 
 TextBox2.Text = TextBox2.Text & Chr(KeyAscii) 
 
 'To handle keyboard combinations (using SHIFT, 
 'CONTROL, OPTION, COMMAND, and another key), 
 'or TAB or ENTER, use the KeyDown or KeyUp event. 
End Sub 
 
Private Sub UserForm_Initialize() 
 Move 0, 0, 570, 380 
 
 TextBox1.Move 30, 40, 220, 160 
 TextBox1.MultiLine = True 
 TextBox1.WordWrap = True 
 TextBox1.Text = "Type text here." 
 TextBox1.EnterKeyBehavior = True 
 
 
 TextBox2.Move 298, 40, 220, 160 
 TextBox2.MultiLine = True 
 TextBox2.WordWrap = True 
 TextBox2.Text = "Typed text copied here." 
 TextBox2.Locked = True 
 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.