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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:39:27  来源:igfitidea点击:

How to add definition of WithEvents variable which require Handles clause

vb.netevents

提问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:

之后,我可以aDgvForm.
除非在这种情况下:

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. And aDgvvariable after Handlesclause 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:

最小的答案是添加WithEventsaDgv

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 the Friendword
  • Done
  • 突出显示代码中带有蓝色下划线的变量
  • F12
  • 它会带你到 designer.vb
  • 然后您将立即看到之前突出显示的变量
  • 现在只需在WithEvents单词后面加上Friend单词
  • 完毕