VB.NET 模拟按钮点击,如何使其恢复正常?



我对这段代码有困难,我解释道: 我创建了一个文本框,当我按回车键时,它模拟单击按钮。

Sub TextBox1KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.Enter Then
'simulate to press Button
Button1Click(sender,New EventArgs)
Else
'code to return to normal
End If
End Sub

它工作正常,但是如何使其恢复正常?我的意思是,当我单击按钮时,它没有反应。 请等待您帮助

解决此问题

调用事件过程通常不是一个好主意。将代码从 Button1 中移出。单击到单独的 sub,然后从两个位置调用它。

Private Sub TextBox1_KeyDown_1(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
'simulate to press Button
MyButtonSub()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MyButtonSub()
End Sub
Private Sub MyButtonSub()
MessageBox.Show("Button One was clicked or pretend clicked.")
End Sub

最新更新