Click here to Skip to main content
15,881,711 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I'd like to launch a method when all instances of all form classes are created.
I tried to unsuccessfully search for an event on Application (like FormCreated or something else).

The only way I found is to create an object inheriting from the Form object, write a load event, and replace all inherition of all form class definitions. What I don't like to do is to go on all of my form definitions. There is a way to do that?

Thank you
Posted

I did some testing with this, and it looks like you can use a message filter for this:
C#
// in Program.cs:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        CreatedFormMessageFilter filter = new CreatedFormMessageFilter();
        // use the OpenFormMessageFilter.FormCreated event to see opening forms
        Application.AddMessageFilter(filter);

        Application.Run(new Form1());
    }
}
class CreatedFormMessageFilter : IMessageFilter
{
    EventHandler _formCreated;
    public event EventHandler FormCreated
    {
        add
        {
            _formCreated += value;
        }
        remove
        {
            _formCreated -= value;
        }
    }
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == 799)
        {
            if (_formCreated != null)
            {
                _formCreated(this, EventArgs.Empty);
            }
        }
        return false;
    }
}

The above code is based on a message filter. A filter can be used for, as the name says, filtering specific messages, but this means that the messages pass through this method. I did some testing, and it looks like message ID 799 is the one you need.
 
Share this answer
 
First of all, to "raise" some method (methods are not really "raised" they are just called), you have to have such method. If you need to create such method, create it in some form class and make all other forms inherit this class.

Now, if you have many form instances or unknown number of them, you probably need to iterate all of them. This property gives you all open forms of some Application instance:
https://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.formcollection%28v=vs.110%29.aspx[^].

So, just iterate FormCollection using foreach loop and call the method you want. If this is the method you created in a base class, unfortunately, you have to typecast each form instance to your base class type.

That's all. Problem solved.

—SA
 
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