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

tom on January 24th, 2009

Visual basic . NET!

This occurs with some code as follows:

[code]
Private Sub button7_Click_1(ByVal sender As Object, ByVal e As EventArgs) Handles button7.Click
' do some stuff
End Sub
[/code]

instead you need to add a line above it, so completed it shall read as :

[code]
Public WithEvents button7 As Button
Private Sub button7_Click_1(ByVal sender As Object, ByVal e As EventArgs) Handles button7.Click
'do some stuff
End Sub
[/code]

Now remember if the end of the private sub says Handles cboStuff.SelectedIndexChange or some stuff, you need to declare the withevents as

Public WithEvents cboStuff as ComboBox

Hope it helps.


Comments are closed.