【问题标题】:Display a form from the main form on startup在启动时从主窗体显示一个窗体
【发布时间】:2011-12-31 17:23:40
【问题描述】:

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

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 在主窗体后面。如何将其放在前面?

【问题讨论】:

  • 您是否有一个无限线程在第二个窗体上运行并锁定它?
  • 不,这只是一个指令序列。

标签: c# .net winforms


【解决方案1】:

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

Z 顺序问题也是(部分)由此引起的,主窗体尚不可见,因此 BringToFront() 不起作用。使用 Show(this) 使 InitForm 成为始终显示在主窗体前面的自有窗口(推荐),或者改用 Shown 事件。

【讨论】:

    【解决方案2】:
    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?

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        您可以通过设置此属性将 Init 表单置于最前面

        init.TopMost=true 
        

        它对我有用,检查一下,

        【讨论】:

          猜你喜欢
          • 2015-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-31
          • 2012-10-08
          • 1970-01-01
          相关资源
          最近更新 更多