不可见父窗体的子窗体永远不会显示

本文关键字:窗体 永远 显示 | 更新日期: 2023-09-27 17:50:35

我有一个主表单,它是不可见的,并且在某个时候创建了一个子表单。子表单在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";
在主表单中,我创建了Form2,如下所示:
    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();

我遇到的问题是,Form2实际上是闪烁的,但随后神奇地消失了。欢迎提出任何建议。

谢谢你的建议,Alex

不可见父窗体的子窗体永远不会显示

Alex -我认为这是你的问题:

如何在自己的线程上创建表单,并在整个应用程序生命周期中保持打开状态

公认的答案是非常正确的,因为我也在我的应用程序中使用它,并且已经忘记了它。=)

"您需要在新创建的线程上启动一个消息循环。你可以通过调用Application.Run(form)来实现。"