There's a bug in Windows Forms that makes a form disappear from the Application.OpenForms collection. This will happen when you assign the ShowInTaskbar, FormBorderStyle, Min/MaximizedBox, RightToLeftLayout, HelpButton, Opacity, TransparencyKey, ShowIcon or MdiParent property after the window was created. These properties are special in that they are specified as style flags in the native CreateWindowEx() call. This sample form demonstrates the bug:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
button1.Click += button1_Click;
}
private void button1_Click(object sender, EventArgs e) {
Console.WriteLine(Application.OpenForms.Count);
this.ShowInTaskbar = !this.ShowInTaskbar;
Console.WriteLine(Application.OpenForms.Count);
}
}
Windows Forms must re-create the window the apply the changed property. That has side effects beyond the very noticeable flicker, one of them is that the Application class loses track of the form. Avoid the bug by setting the property only in the constructor, never in any event handlers.
In general, avoid relying on OpenForms due to this bug. Give the class that needs to display the message box a reference to the form instance through its constructor. MessageBox usually figures out a parent window by itself correctly btw.