如何在C#中执行此功能 [英] How to this function is action in C#

查看:92
本文介绍了如何在C#中执行此功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VB6中有一个,并且转换为C#,但是它不起作用,我也不知道它是如何工作的.我希望你能帮助我.
谢谢吗?

这是Vb6中的usercontrol代码:

I have a in VB6 and I converted to C# but it is not work and I do not understand How it work. I hope that you can help me.
thanks?

this is code of usercontrol in Vb6:

Event CloseClick()
Private Sub btnClose_Click()
   RaiseEvent CloseClick                                   
End Sub



这是C#中的代码:



and this is code in C#:

public event CloseClickEventHandler CloseClick;
        public delegate void CloseClickEventHandler(System.Object Sender, System.EventArgs e);
        private void btnClose_Click(System.Object eventSender, System.EventArgs eventArgs)
        {
            if (CloseClick != null) {
                CloseClick(this, null);
            }
           
        }

推荐答案

您是要使其正常工作还是想知道其工作原理?

您尝试编写代码以表明两者都需要.

您不需要委托声明.由于Click甚至已经在库中定义,因此已经定义了适当的委托类型.针对事件定义的标准建议说,第一个参数的类型应为System.Object,第二个参数应为类型System.EventArgs或派生.对于控件,将传递运行时类型,因为第一个参数的类型与触发事件的控件的类型相同.也可以是其他内容,例如MenuItem(不是从System.Windows.Forms.Control派生的).

Do you want to make it working or want to know how it works?

You attempt to code this shows you need both.

You don''t need delegate declaration. As Click even is already defined in the library, appropriate delegate type is already defined. A standard recommendations for event definition say that first argument should of the the type System.Object, a second one — if the type System.EventArgs or derived. For controls, a run-time type passes as the first argument is of the same type as the control firing the event. It can be something else, like MenuItem (which is not derived from System.Windows.Forms.Control).

MyButton.Click += delegate(object sender, System.EventArgs eventArgs) {
    System.Windows.Forms.MessageBox.Show(
        string.Format("{0} clicked!", (Button)sender.Text));
} //MyButton.Click



最有可能的是,您使用的是C#v.3或更高版本,因此lambda语法更简单,您无需指定参数类型,因为它们是从事件类型从编译器已知的委托类型自动推断出来的.



Most likely, you''re using C# v.3 or later, so lambda syntax is simpler, you need not specify the types of arguments as they are automatically inferred from delegate type known to the compiler from event type.

MyButton.Click += (sender, eventArgs) => {
    //same thing
} //MyButton.Click



在显示表单之前,应随时调用这些代码片段.通常,我通常会在表单的构造函数中以及所有其他设置和布局调整中最终完成此操作.

解释所有可能花费整篇文章的内容.在常规帮助/MSDN中查找委托,事件和匿名委托的概述,这足够了.我多次回答类似的CodeProject问题,例如: Lambda表达式与动作之间的差异 [



These code fragment should be called anytime before your form is shown; I usually do it in the constructor of the form, at the very end, as well as all other settings and layout adjustments.

Explaining all that could take a whole article. Look for overview of delegates, events and anonymous delegates in regular help/MSDN, it should be enough. I answered to similar CodeProject Questions number of times, see, for example: What are the advantages of delegates in C#.NET?[^], difference between Lambda expression and Action[^], look at other answers and recommended reading as well.

—SA


这篇关于如何在C#中执行此功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆