在C ++ CLI中将事件作为函数调用 [英] Calling an Event as a Function in C++ CLI

查看:61
本文介绍了在C ++ CLI中将事件作为函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,



此应用程序适用于Windows(7)



我有一个按钮在我的应用程序中加载文本文件中的设置。我还希望在表单初始化期间调用此事件。



有什么是



Good Afternoon,

This application is for Windows (7)

I have a button in my application which loads settings from a text file. I also want this event to be called during the initialization of the form.

What is have is

Form1(void)
{
    InitializeComponent();

    load_button_Click(this, ...);
}







其中功能如下:






where the function is:

private: System::Void load_button_Click(System::Object^  sender, System::EventArgs^  e) {
...
}









我需要传递系统: :EventArgs ^ e 到这个函数,我完全迷失了如何做到这一点。



我知道我可以放置所有的代码将事件转换为seaparte函数并从事件和初始化期间调用,但我想知道是否有办法调用偶数函数。



谢谢,

-D





I need to pass System::EventArgs^ e to this function and I''m completely lost on how to do that.

I know that I can place all the code from the event into a seaparte function and call that from both the event and during initialization, but I wanted to know if there is a way to call the even function instead.

Thanks,
-D

推荐答案

没有召唤事件这样的概念。一个事件实例可以调用,它调用当前在调用列表中找到的所有事件处理程序



而不是,这是真正的阻止者:你永远不能从n调用事件实例完全没有,除了声明事件实例的类的代码。与常规委托实例相比,这是事件的限制之一。您甚至无法创建派生类并调用继承甚至它的代码。这实际上是一个非常重要的万无一失的功能。该怎么办?当然,不要试图扮演那些傻瓜之一的角色。不,您不能直接调用您未声明的事件,例如 Button.Click 这是因为你真的不需要它。



(实际上,控件可能有方法调用,甚至你可以调用。例如,你可以派生一个类并调用方法 OnClick 。你没有指定什么是 Button 类型,还有一个类似的名字,所以请在文档中找到你可能感兴趣的成员。这不是真正的解决方案。)



事情的本质实际上就在这里:你不是真的想要调用这个事件。 实际上,您只想获得与点击按钮时相同的效果。您是否已经获得了这个想法?您必须创建一个单独的方法,您需要在单击时调用。然后,添加一个偶数处理程序(使用''+ =''运算符),并从事件句柄调用此方法,不做任何其他事情。现在,如果您在其他地方调用此方法,此调用将获得与实际点击相同的效果。很简单,不是吗?



如果您多想一点,您将理解使用.NET事件实例的这种万无一失的功能:它有助于您要从类事件的处理程序中隔离类实例的内部行为。它刺激你做好事(上面描述的那个:-))而不做坏事。







另见我过去对类似问题的回答: c#网络窗口形式 [ ^ ]。



另请参阅我过去对相关主题的回答,例如使用事件:

如何在特定按钮点击时调用keydown事件 [ ^ ],

[解决]如何添加事件for C#Control [ ^ ],

关于用户控件,嵌套控件和封装的问题。 [ ^ ],

WPF:如何在自定义控件中使用事件 [ ^ ],

http://www.codeproject.com/script/Answers/Post.aspx?aid=461004 [ ^ ],

代表和回调 [ ^ ],

复制EventHandler [ ^ ]。



-SA
There is no such concept as "call an event". An event instance can be invoked, which calls all the event handlers currently found in its invocation list.

And not, here is the real blocker: you can never invoke an event instance from nowhere at all, except the code of the class declaring the event instance. This is one of the limitations of events, compared to "regular" delegate instances. You cannot even create a derived class and invoke the inherited even it its code. This is actually a very important fool-proof feature. What to do? Of course, don''t try to play the role of one of those fools. No, you cannot directly invoke an event that you did not declare, such as Button.Click. This is because you really don''t need it.

(Actually, controls may have methods invoking an even you can call. For example, you can derive a class and call the method OnClick. You did not specify what is the Button type exactly, there are more then one type named like that, so please find the members you might be interested in by yourself, in documentation. This is not the real solution.)

The essence of things is actually here: you don''t really want to invoke the event. In reality, you simply want to get the same effect as when click a button. Are you getting the idea already? You have to create a separate method you need to call on click. Then, add an even handler (using ''+='' operator), and call this method from the event handle and don''t do anything else. Now, if you call this method elsewhere, this call will get you the same effect as the actual click. Simple, isn''t it?

If you think a bit more, you will understand the use of this fool-proof feature of .NET event instances: it helps you to isolate inner behavior of a class instance from handlers of its events. It stimulates you to do good things (the one is described above :-)) and not to do bad things.



See also my past answer to a similar question: c# networking windows form[^].

See also my past answers on related topics like using events:
how to call keydown event on particular button click[^],
[Solved] How to add Event for C# Control[^],
A question about usercontrols, nested controls and encapsulation.[^],
WPF : How to Use Event in Custom Control[^],
http://www.codeproject.com/script/Answers/Post.aspx?aid=461004[^],
Delegates and Callbacks[^],
Copying an EventHandler[^].

—SA


为了便于阅读,你应该避免打电话用于执行公共代码的事件处理程序。如果从很多地方使用相同的代码,则声明一个函数并从两个事件处理程序中调用该函数。



这样,你知道从一个事件调用一个事件真实的事件和预期的事件。也就是说,只有当用户实际点击了加载按钮时才会调用事件处理程序load_button_click。



例如,你可以调用一个函数来自构造函数和事件处理程序的DoLoad()。名为事件处理程序的函数应仅用作该特定事件的处理程序。如果处理程序在多个类似控件之间共享(例如10个复选框,它们将设置已修改标志),则处理程序名称不应包含一个特定的控件名称,而是更通用的一些。



作为附注,如果您声明一个事件,则可以调用它(在检查 nullptr 之后)。在这种情况下,发件人通常(或转发时的原始发件人)和 EventArgs EventArgs :: Empty ,如果不需要,另外还有一个适当构造的objject。
For readability, you should avoid calling an event handler to execute common code. If same code is used from many place, then declare a function and call that function from both event handlers.

That way, you know that an event is called from a "real" event and the expected one. That is, an event Handler load_button_click would be called only when the user has actually clicked on Load button.

For example, you could call a function DoLoad() from both the constructor and the event handler. A function named like an event handler should only be used as an handler for that specific event. If an handler is shared among multiple similar controls (like 10 checkboxes that would set a "modified" flag), then the handler name should not contain one specific control name but something a bit more generic.

As a side note, if you declare an event, you can call it (after checking for nullptr). In such case, sender is usually this (or sometime the original sender in case of forwarding) and EventArgs is EventArgs::Empty when not required and an appropriatly constructed objject otherwise.


这篇关于在C ++ CLI中将事件作为函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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