vb.net 句柄子句需要在包含类型或其基类型之一中定义的 WithEvents 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15812648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handles clause requires a WithEvents variable defined in the containing type or one of its base types
提问by V Partap Singh Salathia
Protected Sub drp_usertype_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles drp_usertype.SelectedIndexChanged
End Sub
getting error under drp_usertype.SelectedINdexChanged
在 drp_usertype.SelectedINDexChanged 下出现错误
Its a dropdownlist
它是一个下拉列表
回答by J...
When you declare the drp_usertypeobject it must be done as :
当您声明drp_usertype对象时,它必须按以下方式完成:
Private WithEvents drp_usertype As DropDownList
This is the same for
这对于
Private WithEvents drp_usertype As New DropDownList
etc...
等等...
The WithEventskeyword allows the control to hook up events, using the Handlessyntax, with its owner. When adding components in the designer it automatically generates this for you but when creating them yourself you must include the WithEventsif you intend to use events with the component.
的WithEvents关键字允许控制挂钩事件,使用Handles的语法,用它的主人。在设计器中添加组件时,它会自动为您生成此信息,但在您自己创建它们时,您必须包含WithEventsif 您打算将事件与组件一起使用。
If you do not declare an object WithEventsthen handlers must be assigned programmatically as they are in C# using AddHandler-- see : AddHandler
如果您不声明对象,WithEvents则必须以编程方式分配处理程序,因为它们在 C# 中使用AddHandler-- 请参阅:AddHandler
回答by Tony L.
This error would also thrown if there were no control defined with the name "drp_usertype".
如果没有使用名称“drp_usertype”定义的控件,也会抛出此错误。
I ran into this by copying functionality from one page to another and forgetting to rename the control after the "handles" key word.
我通过将功能从一个页面复制到另一个页面并忘记在“句柄”关键字后重命名控件而遇到了这个问题。
Obviously, this wasn't your issue but I thought I'd post it for the benefit of anyone else who gets this error.
显然,这不是您的问题,但我想我会发布它以让其他遇到此错误的人受益。

