Search code examples
c#.netwindowswinformsinvisible

Child form of invisible parent never being shown?


I have a main form, which is invisible and at some point creates a child form. That child form looks like this in designer.cs:

        this.listBox1 = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        // 
        // listBox1
        // 
        this.listBox1.FormattingEnabled = true;
        //this.listBox1.Location = new System.Drawing.Point(0, 0);
        this.listBox1.Name = "listBox1";
        this.listBox1.Size = new System.Drawing.Size(255, 147);
        this.listBox1.TabIndex = 0;
        // 
        // Form2
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoSize = true;
        this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.ClientSize = new System.Drawing.Size(502, 384);
        this.ControlBox = false;
        this.Controls.Add(this.listBox1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "Form2";
        this.Text = "Form2";

and in the main form I'm creating the Form2 as follows:

    Form2 a = new Form2(new Point(0, Screen.PrimaryScreen.WorkingArea.Height / 2)); //This is the location
                        Thread thtt = new Thread((ThreadStart)delegate()
                            {
                                a.CreateControl();
                                a.Show();
                                TimeSpan slept = TimeSpan.Zero;
                                while (!a.Created && slept < TimeSpan.FromMilliseconds(2000))
                                {
                                    Thread.Sleep(99);
                                    slept += TimeSpan.FromMilliseconds(99);
                                }
                                if (!a.Created)
                                {
                                    MessageBox.Show("after 2 sec no creation?");
                                    System.Diagnostics.Debugger.Break();
                                }
                                else
                                {
                                    //a.Show();
                                    a.Invoke((MethodInvoker)delegate()
                                    {

                                        a.TopMost = true;
                                        a.Location = new Point(0, Screen.PrimaryScreen.WorkingArea.Height / 2);
                                        Cursor.Position = new Point(a.Location.X + 10, a.Location.Y + 10);
                                        a.AddAndAct(minimized);

                                    });
                                }
                                aa = a;
                            });
                        thtt.IsBackground = true;
                        thtt.Start();

The problem I'm encountering is, that the Form2 is actually flashing, but then disappears magically. Any suggestions appriciated.

Thanks in advice, Alex


Solution

  • Alex - I think this is your problem:

    How to create a form on its own thread and keep it open throughout application lifetime

    The accepted answer is very true, for I use it in my applications as well, and had forgotten about it. =)

    "You will need to start a message loop on the newly created thread. You can do that by calling Application.Run(form)."