Click here to Skip to main content
13,095,921 members (53,698 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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


    load_button_Click(this, ...);

where the function is:

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

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.

Posted 24-Apr-13 10:43am
Updated 24-Apr-13 10:56am
Sergey Alexandrovich Kryukov 24-Apr-13 16:49pm
What's the UI library?
dmitryponv 24-Apr-13 16:58pm
Windows 7 , I don't know what you mean by UI, is this Windows GDI?
Sergey Alexandrovich Kryukov 24-Apr-13 17:04pm
What, you never heard that the term UI is used? "User Interface".

I already answered, but what's you UI library? GDI is usually used by System.Windows.Form. Is it Forms? In other words, if you mention "Button", you should provide its exact type, fully qualified type name.
As soon as you are asking about UI, you should always specify it. There are few unrelated libraries. No one is going to give your the variants of answers for all of them.
dmitryponv 24-Apr-13 18:13pm
Yes,standard C++ System.Windows.Forms, nothing third party.
dmitryponv 24-Apr-13 18:21pm
Thank you for the answer, I now understand why events cannot be used as methods.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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[^],[^],
Delegates and Callbacks[^],
Copying an EventHandler[^].

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100