c#mainui落后于其他窗口,问题最顶层

时间:2016-10-25 07:58:29

标签: c# topmost

我遇到了最高级别的问题,找到了“有效”的解决方案,但看起来并不那么好。还有另一种“更清洁”的方法来解决这个问题吗? 这是我的代码:代码中的事件评论。

    OrderTemplateView template;
private void toolStripButton4_Click(object sender, EventArgs e)
    {
        if (template != null)
        {
            template.Close(); //must close to trigger close event.
            template.Dispose();
        }
        mainUi.TopMost = true; // must set my mainUi topMost here othervise it drops in the background of other windows open at the computer.
        template = new OrderTemplateView(this);
        template.TopMost = true;// must set my dialog topmost othervise it drops behind my mainUi
        template.StartPosition = FormStartPosition.CenterParent;
        mainUi.TopMost = false;//must release my topmost so other windows on the computer can be called to front.
        template.TopMost = false;
        template.ShowDialog();
    }

更新了执行相同工作的代码:

 private void toolStripButton4_Click(object sender, EventArgs e)
    {
        if (template != null)
        {
            template.Close();
            template.Dispose();
        }
        template = new OrderTemplateView(mainUi);
        template.StartPosition = FormStartPosition.CenterParent;
        template.ShowDialog(mainUi);
    }

`

1 个答案:

答案 0 :(得分:0)

请尝试以下方法,而不是设置TopMost:

  1. 删除对TopMost

  2. 的所有引用
  3. 致电mainUi.BringToFront()

  4. 调用template.ShowDialog(mainUi),注意父控件已传递给对话框。