Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

vb.net

I have a VB.net project that was converted from c# using Sharpdevelop. When I open the project, I can run it fine. Then I added a button to the form and all of a sudden 12 errors popped up saying:

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

for lines that look like this:

Private Sub topListButton_Click(sender As Object, e As EventArgs) _
    Handles topListButton.Click

This is the code that was converted from c#. I am not sure why it ran originally but then bombed after I made a change to the project.

I tried removing the part of the lines with:

Handles topListButton.Click

but now the button clicks don't fire. Any clues would be greatly appreciated.

like image 503
user3229570 Avatar asked Dec 21 '25 23:12

user3229570


1 Answers

VB.Net has some strange bugs (or features, depending on who's asking).
if you want to use the Handles keyword to attach event handlers, you must declare the object raising the event with the WithEvents keyword.
The strange thing about it is that the events will be raised with or without the WithEvents declaration, it's only purpose is to allow you to use the Handles keyword. you can still attach events to handlers using the Add handler statement.
You can see a code example here.

like image 164
Zohar Peled Avatar answered Dec 23 '25 12:12

Zohar Peled



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!