VB.NET中文本框的按键事件

时间:2010-07-23 06:22:44

标签: vb.net

我必须在文本框中使用按键事件。该事件的代码如下所示:

 Private Sub TextBox1_Keydown(ByVal sender As Object, ByVal e As
 System.Windows.Forms.KeyEventArgs) Handles textBox1.KeyDown   
 If e.KeyCode = keys.Enter Then
    MessageBox.Show("Hello")   
 End If
 End Sub

但它会出现以下错误:

Handles clause requires a WithEvents variable defined in the containing type  
or one of its base types. (BC30506) 
这是什么意思?我必须为此做些什么?

请帮帮我。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

检查文本框是否使用WithEvents关键字定义 示例here

编辑:对于您的情况,定义应该是这样的 WithEvents TextBox1 as TextBox

答案 1 :(得分:0)

在{formname} .Designer.vb文件中检查是否有类似的内容声明:

Friend WithEvents TextBox1 As System.Windows.Forms.TextBox