我的设计形式有这个。
我的代码是以下
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RichTextBox1.AppendText(TextBox1.Text)
End Sub
Private Sub TextBox1_Keyup(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Enter Then
Call Button2_Click(sender, e)
End If
End Sub
问题是当我点击文本框,在其中键入内容然后按回车键时我会听到哔哔声。那声音就是我要禁用的声音。
我还注意到,如果我只是输入文本框并单击按钮我就听不到声音,只要我点击文本框,在其中输入内容并按下回车键就会发出声音。
修改
通过深入研究,我意识到叮当声不是来自按下按钮,而是来自richtextbox的最后一行。当我们按下key_down时,我们可以听到同样的声音,我们在richtextbox的最后一行。 How do I disable it?
答案 0 :(得分:2)
只需将Form的AcceptButton()属性设置为Button2即可。那么你根本不需要KeyUp代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AcceptButton = Button2
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RichTextBox1.AppendText(TextBox1.Text)
End Sub
' Don't need this code anymore:
'Private Sub TextBox1_Keyup(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
' If e.KeyCode = Keys.Enter Then
' Call Button2_Click(sender, e)
' End If
'End Sub