gpt4 book ai didi

c# - MouseEventHandler 附加参数 - 无法定义 "sender"和 "e"

转载 作者:太空狗 更新时间:2023-10-30 00:52:51 25 4
gpt4 key购买 nike

我想将一个参数传递给我试图用这段代码实现的事件处理程序:

    private void openInputImagesToolStripMenuItem_Click(object sender, EventArgs e)
{
...
pb.MouseDoubleClick += new MouseEventHandler((sender,e) => showLargeImage(sender,e,imageIndex));
}

private void showLargeImage(object sender, MouseEventArgs e, int imageIndex)
{
...
}

但是,我在适配器内的“sender”和“e”变量上收到错误消息:

A local variable named 'sender' cannot be declared in this scope because it would give a different meaning to 'sender', which is already used in a 'parent or current' scope to denote something else.

A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'sender', which is already used in a 'parent or current' scope to denote something else.

我该如何解决这个问题?

最佳答案

您不能将 lambda 方法的参数命名为 sendere,正如编译器告诉您的那样。

这会在 openInputImagesToolStripMenuItem_Click 方法的参数 sender 和 lambda 方法的参数之间产生歧义。

就用别的名字吧,比如

pb.MouseDoubleClick += new MouseEventHandler((s, e1) => showLargeImage(s, e1,imageIndex));

关于c# - MouseEventHandler 附加参数 - 无法定义 "sender"和 "e",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19511401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com