System.EventArgs 不包含 Button 的定义 - C#
System.EventArgs Doesn't contain the definition of Button - C#
我的 winform 上有一个 DataGridView
,我在其中显示了 5 列和几行,我想添加一个功能,如果我右键单击任何行,它会显示一个菜单以查看更多内容有关该记录的详细信息。
但是当我写 e.Button
时出现错误
System.EventArgs Doesn't contain the definition of Button and no extension method button accepting the first argument of type System.EventArgs could be found (are you missing any directive or an assembly reference?)
您的委托处理程序方法应该使用 DataGridViewCellMouseEventArgs
DataGridView
的MouseClick
事件实际上提供了一个MouseEventArgs
参数。您仅使用 EventArgs
.
声明了 datagridview1_MouseClick
方法
将其更改为
protected void datagridview1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == ...
它应该可以工作(如果您使用 CellMouseClick
而不是 MouseClick
,请使用 DataGridViewCellMouseEventArgs
而不是 MouseEventArgs
)。
附带说明:添加 "wrongly" 声明的处理程序(如 datagridview1.MouseClick += datagridview1_MouseClick
时不会出现编译错误,因为 MouseEventArgs
派生自 EventArgs
,因此编译器对该赋值没有问题。当 e
声明为 EventArgs
时,当您尝试通过 e
访问 MouseEventArgs
实例的属性时,就会出现问题,因为 EventArgs
不知道MouseEventArgs
派生的属性。
P.S.: 请 post 您的代码作为下一个问题的文本。与图像相比,我们阅读或重新使用以重现错误更好。在那张图片中,我看不到你是否使用了 MouseClick
或 CellMouseClick
事件
我的 winform 上有一个 DataGridView
,我在其中显示了 5 列和几行,我想添加一个功能,如果我右键单击任何行,它会显示一个菜单以查看更多内容有关该记录的详细信息。
但是当我写 e.Button
System.EventArgs Doesn't contain the definition of Button and no extension method button accepting the first argument of type System.EventArgs could be found (are you missing any directive or an assembly reference?)
您的委托处理程序方法应该使用 DataGridViewCellMouseEventArgs
DataGridView
的MouseClick
事件实际上提供了一个MouseEventArgs
参数。您仅使用 EventArgs
.
datagridview1_MouseClick
方法
将其更改为
protected void datagridview1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == ...
它应该可以工作(如果您使用 CellMouseClick
而不是 MouseClick
,请使用 DataGridViewCellMouseEventArgs
而不是 MouseEventArgs
)。
附带说明:添加 "wrongly" 声明的处理程序(如 datagridview1.MouseClick += datagridview1_MouseClick
时不会出现编译错误,因为 MouseEventArgs
派生自 EventArgs
,因此编译器对该赋值没有问题。当 e
声明为 EventArgs
时,当您尝试通过 e
访问 MouseEventArgs
实例的属性时,就会出现问题,因为 EventArgs
不知道MouseEventArgs
派生的属性。
P.S.: 请 post 您的代码作为下一个问题的文本。与图像相比,我们阅读或重新使用以重现错误更好。在那张图片中,我看不到你是否使用了 MouseClick
或 CellMouseClick
事件