0

We have a big windows forms solution and need to get an event every time a form is opened or closed for telemetry. All forms should be tracked across the entire application. I found a collection of all the open forms through Application.OpenForms but I don't think it is possible to observe it for changes since it is a FormCollection.

Is there any way of doing this with minimal changes to the existing forms? Perhaps something related to the application process?

Rafael
  • 130
  • 10
  • 1
    "with minimal changes to the existing forms" See [Jimi's Answer Here](https://stackoverflow.com/a/55960110/2330053) that does it all from `program.cs`. – Idle_Mind Feb 02 '23 at 20:31

1 Answers1

-1

You can create a static boolean variable in each form and set the variable to true when the form is shown(form_shown_event) and set it to false when the form is closing(form_closing_event). Then you can access the variable as Form1.formOpen to check if the form is already open or not.

A sample for your code:

public partial class Form1 : Form
    {

        public static bool formOpen = false;
        public Form1()
        {
            InitializeComponent();
            this.Shown += Form1_Shown;
            this.FormClosing += Form1_FormClosing;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            formOpen = false;
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            formOpen = true;
        }
    }