Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To register an event to every form in the project would require that you have instances of the forms created. Then you need to assign a delegate to the Load event of each form instance. </p> <p>Assigning the Load event to the type itself is not possible, an instance of the type is required to assign the handler to the Load event. A part of the code could be included in the constructor to register the load event every time a form instance is created.</p> <p>Here is a code snippet of a project containing 3 form classes (Form1 (startup form), Form2, Form3). </p> <pre><code>public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { LoadEventRegister loadEventRegister = new LoadEventRegister(); Form[] formInstances = new Form[] {new Form2(), new Form3()}; loadEventRegister.RegisterLoadOnForms(formInstances); foreach (Form formInstance in formInstances) { formInstance.Show(); } } } public class LoadEventRegister { public void RegisterLoadOnForms(IEnumerable&lt;Form&gt; formInstances) { foreach (Form formInstance in formInstances) { EventInfo eventInfo = formInstance.GetType().GetEvent("Load"); Type eventHandlerType = eventInfo.EventHandlerType; MethodInfo eventHandler = this.GetType().GetMethod("Generic_Load"); Delegate d = Delegate.CreateDelegate(eventHandlerType, this, eventHandler); eventInfo.AddEventHandler(formInstance, d); } } public void Generic_Load(object sender, EventArgs e) { MyCustomLoad(); } private void MyCustomLoad() { // Do something } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload