Why is only the first shown window focusable

Posted by Miha Markic on Stack Overflow See other posts from Stack Overflow or by Miha Markic
Published on 2009-07-02T15:16:56Z Indexed on 2010/05/08 11:08 UTC
Read the original article Hit count: 200

Filed under:
|

Imagine the code below. Only the first window appears on the top, all of subsequent windows won't nor can they be programatically focused for some reason (they appear in the background). Any idea how to workaround this? BTW, static methods/properties are not allowed nor is any global property.

[STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Thread t1 = new Thread(CreateForm);
        t1.SetApartmentState(ApartmentState.STA);
        t1.Start();
        t1.Join();

        t1 = new Thread(CreateForm);
        t1.SetApartmentState(ApartmentState.STA);
        t1.Start();
        t1.Join();
    }

    private static void CreateForm()
    {
        using (Form f = new Form())
        {
            System.Windows.Forms.Timer t = new System.Windows.Forms.Timer
            {
                Enabled = true,
                Interval = 2000
            };
            t.Tick += (s, e) => { f.Close(); t.Enabled = false; };

            f.TopMost = true;
            Application.Run(f);
        }
    }

© Stack Overflow or respective owner

Related posts about winforms

Related posts about focus