vb.net 如何添加需要 Handles 子句的 WithEvents 变量的定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13994199/
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
How to add definition of WithEvents variable which require Handles clause
提问by Wine Too
In order to make my program more elegant and better organized in concrete case I would like to change DataGridView1variablewith referenced variable on top of my Form1class
为了使我的程序在具体情况下更优雅和更好地组织,我想DataGridView1variable在我的Form1班级顶部使用引用的变量进行更改
Private aDgv As DataGridView
And assign value in Form1_Load
并赋值 Form1_Load
aDgv = DataGridView1
After that I can use aDgvvariable across that Form.
Except in such case:
之后,我可以aDgv在Form.
除非在这种情况下:
Private Sub aDgv_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles aDgv.KeyDown
aDgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect
'etc...
End Sub
Where I get an error:
我收到错误的地方:
Handles clause requires a
WithEventsvariable defined in the containing type or one of its base types. AndaDgvvariable afterHandlesclause is blue underlined.
句柄子句需要
WithEvents在包含类型或其基类型之一中定义的变量。而aDgv之后的可变Handles条款是蓝色的下划线。
What to do to get rid of error and get Handles aDgv.SomeEventto work?
Of course, with referenced aDgvinstead of original control name DataGridView1.
如何摆脱错误并开始Handles aDgv.SomeEvent工作?
当然,用引用aDgv代替原来的控件名称DataGridView1。
回答by Mark Hurd
The minimal answer is to add WithEventsto aDgv:
最小的答案是添加WithEvents到aDgv:
Private WithEvents aDgv As DataGridView
回答by montesajudy
The answer of Mark Hurd also works for me. But here's a detailed way on how to do that for beginners like I am.
马克赫德的回答也适用于我。但这里有一个关于如何为像我这样的初学者做到这一点的详细方法。
- Highlight the variable in your code that has a blue underline
- Press F12
- It will take you to the
designer.vb - You will then immediately see the variable that was highlighted earlier
- Now just put
WithEventsword after theFriendword - Done
- 突出显示代码中带有蓝色下划线的变量
- 按 F12
- 它会带你到
designer.vb - 然后您将立即看到之前突出显示的变量
- 现在只需在
WithEvents单词后面加上Friend单词 - 完毕

