rogier

rogier

  • NA
  • 2
  • 0

Form.TopMost causes an ObjectDisposedException when closing...

Jun 25 2008 12:26 PM
Hello there,

I have an application that for reasons not really that interesting runs through a single "Program" class and has been written in .NET studio 2003
(Framework 1.1)
This Program class makes sure the main application form is available as a static property of the Program class.
i.e:
class Program
{
    private static Form _mainform;
    public static Form mainform
    {
       get{return mainform;}
    }
        static void Main()
        {
            _mainform = new Form1();
            Application.Run(_mainform);
        }
}

Everything seems to work just fine ... EXCEPT:
as soon as I set the TopMost property of Form1 to true, and I use this.Close() in the FormLoad event, i get the Exception
"Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object named "Form1"."

        private void Form1_Load(object sender, System.EventArgs e)
        {
            //TopMost is set true in InitializeComponents()
            this.Close();
        }

Does anyone know WHY this happens?
I do know that NOT setting TopMost solves the issue, but I'm very curious as to why this happens!



Answers (1)