Hello Every one

I have worked with VB.NET two years back. I have lost little touch with that and trying to get back.
I have to use key down event on text box in VB form.

The code for the event looks like this:

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

I am using sharp development open source IDE.

I am getting the following error while building

Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

when tried to declare like this

private withevents textBox1 as textbox

The following error comes:

'textBox1' is already declared as 'Private WithEvents textBox1 As System.Windows.Forms.TextBox' in this class. (BC30260)

Can any one help me out?
Thanks a lot.

6 Years
Discussion Span
Last Post by Teme64

I haven't used that much SharpDevelop, but I suppose it creates files Form1.vb, Form1.resx and Form1.Designer.vb for a form like VS IDE. There's something wrong in the Form1.Designer.vb file (Form1 is your form's name).

Open the form in the design mode and remove TextBox1 (and that private withevents textBox1 as textbox too). Save and exit the project. Reopen the project, you may get error messages of missing TextBox1. Now, add a textbox to the form, check that it's name is TextBox1 and save the project. Do you still get that error?

If you still get error about missing TextBox1 (or that "Handles clause requires a WithEvents variable defined..." to be precise), close and exit the project and IDE. Locate your project directory and Form1.Designer.vb file (Form1 is your form's name). Open that file with some text editor (Notepad will do). You should see:

Partial Class Form1
    Inherits System.Windows.Forms.Form

and few procedures. After the procedures and before "End Class" statement you should see lines like: Friend WithEvents Button1 As System.Windows.Forms.Button Is there a line: Friend WithEvents TextBox1 As System.Windows.Forms.TextBox ?
If not, add that line (exactly as it is above) somewhere between last "End Sub" and "End Class" lines, save the file and exit editor. Now, start SharpDevelop and open your project.

Remember to backup your project first!


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.