Click here to Skip to main content
15,887,910 members
Please Sign up or sign in to vote.
1.00/5 (1 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

C#
Form1(void)
{
    InitializeComponent();

    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.

Thanks,
-D
Posted
Updated 24-Apr-13 10:56am
v4
Comments
Sergey Alexandrovich Kryukov 24-Apr-13 16:49pm    
What's the UI library?
—SA
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.
—SA
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.

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.

[EDIT]

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
 
Share this answer
 
v6
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900