Method name expected c#

梦想的初衷 提交于 2019-12-02 04:20:57

You can't pass arguments to your method when you subscribing an event handler.You should just specify the method name:

p.Click +=  new EventHandler(P_Click);

Or short notation:

p.Click +=  P_Click;

If you want to trigger this manually and pass some arguments then you should call your method with that arguments:

P_Click(this, new MyEventArgs { design = reader1["desig_prd"].ToString(), 
                                prix = (float)reader1["prix_prd"] 
                              });

But this is usually a bad practice, (triggering event handler methods manually).Instead use PerformClick method after subscribe your event handler and of course create another method for your arguments, and call that method from P_Click:

p.PerformClick();

Change it to this

 p.Click += new System.EventHandler(P_Click);

Use a lambda to close over the data that you want to use in your event handler:

p.Click += (s, args) => 
    P_Click(reader1["desig_prd"].ToString(), (float)reader1["prix_prd"]);

You misunderstood how events work.

p.Click is the event.

P_Click is the event handler.

You don't send the sender and event args - because the event does. So to hook the event all you need is p.Click += new System.EventHandler(P_Click); or event simply p.Click += P_Click;

You don't need MyEventArgs (at least not for this case), and instead, just calculate these values in the event hadler itself:

public void P_Click(Object sender, EventArgs e)
{
    var design = reader1["desig_prd"].ToString();
    var prix = (float)reader1["prix_prd"];
    ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!