[英]Handles clause requires a WithEvents variable defined in the containing type or one of its base types in VB.net
I have a VB.net project that was converted from c# using Sharpdevelop. 我有一个使用Sharpdevelop从c#转换的VB.net项目。 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: 然后我在表单中添加了一个按钮,突然出现12个错误,说:
Handles clause requires a WithEvents variable defined in the containing type or one of its base types Handles子句需要在包含类型或其基类型之一中定义的WithEvents变量
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#. 这是从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. 任何线索将不胜感激。
VB.Net has some strange bugs (or features, depending on who's asking). VB.Net有一些奇怪的错误(或功能,取决于谁在询问)。
if you want to use the Handles
keyword to attach event handlers, you must declare the object raising the event with the WithEvents
keyword. 如果要使用Handles
关键字附加事件处理程序,则必须使用WithEvents
关键字声明引发事件的对象。
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. 奇怪的是,事件将在有或没有WithEvents
声明的情况下引发,它的唯一目的是允许您使用Handles
关键字。 you can still attach events to handlers using the Add handler
statement. 您仍然可以使用Add handler
语句将事件附加到处理Add handler
。
You can see a code example here . 您可以在此处查看代码示例。
保存topListButton_Click
方法的主体(Ctrl-C),删除整个方法,通过双击WinForms设计器中的topListButton
重新创建它,然后再次插入主体(Ctrl-V)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.