简体   繁体   English

在启动时显示主表单中的表单

[英]Display a form from the main form on startup

I want to display a small form when the program is ran. 我想在程序运行时显示一个小表格。 This is the code: 这是代码:

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;
}

The Start() method draws some lines to the list box on the InitForm . Start()方法将一些行绘制到InitForm上的列表框中。

I have two problems: 我有两个问题:

  1. 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; 当程序启动时,列表框没有被填充,我只得到等待的光标,然后显示主窗体并同时填充框;
  2. The InitForm is behind the main form. InitForm位于主窗体的后面。 How do I bring it to front? 我该如何将它放在前面?

Can't see a ListBox in that code. 在该代码中看不到ListBox。 But painting doesn't happen until the UI thread goes idle again, after your Load event handler completes. 但是,直到Load事件处理程序完成之后,直到UI线程再次变为空闲状态,绘画才会发生。 Which also means that the Opacity assignment doesn't accomplish anything. 这也意味着不透明度分配不会完成任何操作。

The Z-order problem is (partly) caused by this too, the main form isn't visible yet so BringToFront() doesn't work. Z顺序问题也是(部分)由此引起的,主要形式尚不可见,因此BringToFront()不起作用。 Use either Show(this) so that InitForm is an owned window that always displays in front on the main form (recommended) or use the Shown event instead. 使用Show(this)可以使InitForm是一个始终在主窗体上始终显示在前面的窗口(推荐),或者使用Shown事件。

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;

What do you expect should happen? 您期望会发生什么? Your UI Thread is busy in executing the code below 您的UI线程正忙于执行以下代码

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

Once it gets freed it shows both the forms and list box populated. 释放后,它会同时显示表单和已填充的列表框。 It is behaving correctly in my opinion 我认为它的行为正确

this.Opacity = 0; Above line won't have any effect because UI thread will execute all the lines first then it will display the UI which means by the time UI shows something this.Opacity = 100; 上面的行没有任何作用,因为UI线程将首先执行所有行,然后将显示UI,这意味着当UI显示此内容时this.Opacity = 100; would have already been executed 本来会被执行

    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?

Why don't you set the Small Form as startup form and load the MainForm in load method of small form? 为什么不将Small Form设置为启动表单,并以Small Form加载方法加载MainForm

You should load the secondary form on a paralell thread. 您应该在paralell线程上加载辅助表单。 So, on the Form1's load event handler you trigger the second thread, showing the Form2. 因此,在Form1的加载事件处理程序上,您将触发第二个线程,显示Form2。

You can bring the Init form to front by setting this property 您可以通过设置此属性将Init表单置于最前面

init.TopMost=true 

It worked for me, check out, 它对我有用,退房,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM