在启动时显示主窗体中的窗体

本文关键字:窗体 显示 启动 | 更新日期: 2023-09-27 18:19:09

我想在程序运行时显示一个小表单。这是代码:

private void Form1_Load(object sender, EventArgs e)
{
    this.Opacity = 0;
    InitForm init = new InitForm();
    init.Show();
    init.BringToFront();
    comm = init.Start();
    this.Opacity = 100;
}

Start()方法在InitForm的列表框上绘制一些线。

我有两个问题:

  1. 程序启动时,列表框没有填充,我只是得到等待光标,然后显示主表单和框
  2. InitForm在主形式后面。我怎么把它拿到前面来?

在启动时显示主窗体中的窗体

在代码中看不到ListBox。但是,在Load事件处理程序完成之后,直到UI线程再次空闲时,才会进行绘制。这也意味着不透明度的分配没有完成任何事情。

z轴顺序问题(部分)也是由这引起的,主要形式还不可见,所以BringToFront()不起作用。使用Show(this)使InitForm成为一个始终显示在主窗体前面的自有窗口(推荐),或者使用Show事件代替。

When the program starts, the list box is not populated, I just get the
 waiting cursor, then the main form is displayed and the box is populated
 at the same time;

你认为会发生什么?你的UI线程正忙于执行下面的代码

this.Opacity = 0;
InitForm init = new InitForm();
init.Show();
init.BringToFront();
comm = init.Start();
this.Opacity = 100;

一旦被释放,它将显示已填充的表单和列表框。在我看来,这是正确的

this.Opacity = 0;上面的行不会有任何影响因为UI线程会先执行所有的行然后它会显示UI这意味着当UI显示一些东西时this.Opacity = 100;已经执行了

    I want to display a small form when the program is ran. The InitForm 
is behind the main form. How do I bring it to front?

为什么不将Small Form设置为启动表单,将MainForm设置为小表单的加载方式?

您应该在并行线程上加载二级表单。因此,在Form1的load事件处理程序中触发第二个线程,显示Form2。

您可以通过设置这个属性将Init表单放在前面

init.TopMost=true 

它为我工作,检查,