【问题标题】:c# mainui drops behind other windows, issue with topmostc# mainui 落在其他窗口后面,最上面的问题
【发布时间】:2016-10-25 07:58:28
【问题描述】:

我遇到了最高级别的问题,找到了一个“有效”的解决方案,但看起来不太好。还有另一种“更清洁”的方法来解决这个问题吗? 这是我的代码:cmets of event in code。

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

`

【问题讨论】:

    标签: c# topmost


    【解决方案1】:

    与其设置 TopMost,不如尝试以下方法:

    1. 删除所有对TopMost的引用

    2. 致电mainUi.BringToFront()

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

    【讨论】:

    • 删除所有对 TopMost 的引用,然后调用 mainUi.BringToFront(),然后调用 template.ShowDialog(mainUi) - 注意我将 mainUi 传递给 ShowDialog() 的调用。
    • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
    • @pylover 我已将答案更新为正确答案。为更正干杯。
    • 在所有格式优化之后,请提醒:您的答案太短,您能解释一下发生了什么以及这个变通方法是如何工作的吗?
    • @pylover 答案简洁、正确且不言自明。如果 OP 希望了解更多关于为什么这样做的信息,那么他们可以自己研究 BringToFront() 和 ShowDialog()。
    猜你喜欢
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    相关资源
    最近更新 更多