Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Windows Events CLI
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) {
...
}
 

 
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 24-Apr-13 11:43am
Edited 24-Apr-13 11:56am
v4
Comments
Sergey Alexandrovich Kryukov at 24-Apr-13 16:49pm
   
What's the UI library?
—SA
dmitryponv at 24-Apr-13 16:58pm
   
Windows 7 , I don't know what you mean by UI, is this Windows GDI?
Sergey Alexandrovich Kryukov at 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 at 24-Apr-13 18:13pm
   
Yes,standard C++ System.Windows.Forms, nothing third party.
dmitryponv at 24-Apr-13 18:21pm
   
Thank you for the answer, I now understand why events cannot be used as methods.
Rate this: bad
good
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 Smile | :) ) 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
  Permalink  
v6
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2014
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