windows WinForm 应用程序事件处理程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4079191/
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-15 15:32:01  来源:igfitidea点击:

WinForm Applications event handler

c#windowswinforms

提问by xbonez

I am just trying my hand at some WinForm Applications and was creating a simple event handler, but I get an error message. Code:

我只是尝试使用一些 WinForm 应用程序并创建一个简单的事件处理程序,但我收到一条错误消息。代码:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public delegate void MyHandler1(object sender, EventArgs e);

        public Form1()
        {
            InitializeComponent();

            List<string> names = new List<string>();
            names.Add("S");
            names.Add("I");
            names.Add("G");

            MyHandler1 onClicked = new MyHandler1(clicked);

            listBox1.DataSource = names;
            listBox1.Click += onClicked;


        }

        public void clicked(object sender, EventArgs e)
        {
            label1.ResetText();
            label1.Text = listBox1.SelectedItem.ToString();
        }
    }

}

Error:

错误:

Error   1   Cannot implicitly convert type 'WindowsFormsApplication1.Form1.MyHandler1' to 'System.EventHandler'

回答by Ani

The reason that your code doesn't compile is that implicit conversions do not exist between different delegate-types, even when the signatures are 'compatible'.

您的代码无法编译的原因是不同委托类型之间不存在隐式转换,即使签名是“兼容的”。

Try either of these:

尝试以下任一方法:

// Implicit method-group conversion, should work from C# 2.0 or later.
// Essentially shorthand for listBox1.Click += new EventHandler(clicked);
listBox1.Click += clicked; 

// Creating a delegate-instance from a 'compatible' delegate,
// a trick I recently learnt from his highness Jon Skeet
listBox1.Click += new EventHandler(onClicked);

As an aside, unless the intention is to learn how to use delegates, I suggest you don't create your own delegate-type when one that comes with the framework will do the job.

顺便说一句,除非目的是学习如何使用委托,否则我建议您不要创建自己的委托类型,因为框架附带的委托类型可以完成这项工作。

回答by Dror Helper

Just use this code instead:

只需使用此代码:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public delegate void MyHandler1(object sender, EventArgs e);

        public Form1()
        {
            InitializeComponent();

            List<string> names = new List<string>();
            names.Add("S");
            names.Add("I");
            names.Add("G");

            listBox1.DataSource = names;
            listBox1.Click += clicked;


        }

        public void clicked(object sender, EventArgs e)
        {
            label1.ResetText();
            label1.Text = listBox1.SelectedItem.ToString();
        }
    }
}

You don't really need the EventHandler1 in order to listen to handle an event with clickedmethod.

您实际上并不需要 EventHandler1 来侦听使用clicked方法处理事件。

回答by Pete

You do not need to create an entirely new delegate type to subscribe to an existing event. The event you are subscribing to already uses the existing System.EventHandlerdelegate type.

您无需创建全新的委托类型即可订阅现有事件。您订阅的事件已使用现有的System.EventHandler委托类型。

You only need to do:

你只需要做:

listBox1.Click += new EventHandler(onClicked);