简体   繁体   English

System.EventArgs不包含Button的定义-C#

[英]System.EventArgs Doesn't contain the definition of Button - C#

I have a DataGridView on my winform and I am showing 5 columns and few rows in it, I want to add a functionality that if I right click on any row it displays me a menu to see more detail about that record. 我在winform上有一个DataGridView ,并在其中显示5列和几行,我想添加一个功能,如果我右键单击任何行,它将显示一个菜单,以查看有关该记录的更多详细信息。 But I am getting an error when I write e.Button that 但是我在写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?) System.EventArgs不包含Button的定义,也没有找到接受System.EventArgs类型的第一个参数的扩展方法button(您是否缺少任何指令或程序集引用?)

在此处输入图片说明

The MouseClick event of DataGridView actually provides a MouseEventArgs argument. 实际上, DataGridViewMouseClick事件提供了一个MouseEventArgs参数。 You declared your datagridview1_MouseClick method only with EventArgs . 您仅使用EventArgs声明了datagridview1_MouseClick方法。

Change that to 更改为

protected void datagridview1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == ...

and it should work (in case you used CellMouseClick instead of MouseClick , use DataGridViewCellMouseEventArgs instead of MouseEventArgs ). 它应该工作(如果你使用的CellMouseClick代替MouseClick ,使用DataGridViewCellMouseEventArgs而不是MouseEventArgs )。


As a side note: you don't get a compilation error when you add your "wrongly" declared handler like datagridview1.MouseClick += datagridview1_MouseClick , because MouseEventArgs is derived from EventArgs , so the compiler has no problem with that assignment. 附带说明:添加“错误”声明的处理程序(例如datagridview1.MouseClick += datagridview1_MouseClick ,不会出现编译错误,因为MouseEventArgs派生自EventArgs ,因此编译器对该分配没有问题。 The problem arises when you try to access the properties of a MouseEventArgs instance via e when e is declared as EventArgs , because EventArgs doesn't know the properties of the MouseEventArgs derivate. 当您将e声明为EventArgs时,尝试通过e访问MouseEventArgs实例的属性时,就会出现问题,因为EventArgs不知道MouseEventArgs派生的属性。


PS: Please post your code as text to your next question. PS:请将您的代码作为文本发布到下一个问题。 It's better for us to read or to re-use for reproducing the error than an image. 与图像相比,对我们而言,读取或重复使用该错误再现更好。 In that image, I can't see if you used the MouseClick or CellMouseClick event 在这个形象,我不能看到,如果你使用的MouseClickCellMouseClick事件

Your delegate handler method should be using DataGridViewCellMouseEventArgs 您的委托处理程序方法应使用DataGridViewCellMouseEventArgs

https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellmouseclick(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/system.windows.forms.datagridview.cellmouseclick(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#-system.eventargs'不包含'graphics'的定义 - C#- system.eventargs' does not contain a definition for 'graphics' “ System.EventArgs”不包含“位置”的定义 - 'System.EventArgs' does not contain a definition for 'Location' MessageBox错误System.EventArgs不包含定义 - MessageBox error System.EventArgs does not contain a definition 尝试通过安全透明方法'Compat.Button1_Click(System.Object,System.EventArgs)'访问安全关键方法'x'失败 - Attempt by security transparent method 'Compat.Button1_Click(System.Object, System.EventArgs)' to access security critical method 'x' failed 为什么我无法访问System.EventArgs的KeyCode成员? - Why am I not able to access the KeyCode member of System.EventArgs? 从“System.EventArgs objArgs”中获取“Text”值 - Get the “Text” value out of “System.EventArgs objArgs” System.EventArgs从哪里获得其值? - From where System.EventArgs get its values? 无法将带有[]的索引应用于类型为'System.EventArgs'的表达式 - Cannot apply indexing with [] to an expression of type 'System.EventArgs' Visual C#错误:System.Windows.Forms.Button不包含ToInt32的定义 - Visual C# Error: System.Windows.Forms.Button does not contain a definition for ToInt32 C#:单选按钮没有定义。IsChecked? - C#: Radio Button doesn't have definition .IsChecked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM