C#,Windows窗体,消息框顶部不工作

问题描述 投票:6回答:8

我有一些消息框,我这样的代码:

MessageBox.Show(new Form(){TopMost=true, TopLevel=True}, "Message","Title", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

对于一个更好的例子,我这​​样做对的FormClosing事件:

private void Example_FormClosing(object sender, FormClosingEventArgs e){
MessageBox.Show(new Form(){TopMost=true, TopLevel=True}, "Really close?"," Program", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}

但是,几乎每次我看到我的消息框之前已经在我的电脑(像Visual Studio的回报)对窗口的变化,它不是用户友好,真的很烦人。

我核实,我的主要形式是不是在最顶层=真,我尝试了最顶层或只是顶层的中StartPosition = FormStartPosition.CenterScreen但是毫无效果。

[更新]

我试过了:

 private void Example_FormClosing(object sender, FormClosingEventArgs e){
    MessageBox.Show(this.Owner, "Really close?"," Program", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
    }

我想对我的窗前我的消息框,并没有改变窗口看到它,因为它就像当前窗口的后面。

你有一个想法,以解决这个问题呢?

c# messagebox topmost
8个回答
4
投票

鉴于你Form的实例,你可以这样调用一个MessageBoxMessageBox.show(form, "Message", "Title");Check the doc for other parameters.

但是,如果你想从后台线程调用这个(例如:BackgroundWorker),你必须使用Form.Invoke()这样的:

form.Invoke((MethodInvoker)delegate
{
   MessageBox.show(form, "Message", "Title");
});

10
投票

像这样做:

MessageBox.Show(
    "Message", 
    "Title", 
    MessageBoxButtons.YesNo, 
    MessageBoxIcon.Warning, 
    MessageBoxDefaultButton.Button1, 
    MessageBoxOptions.DefaultDesktopOnly);

它将把它放在所有其他窗口,包括来自其他进程(这是我想你问的)的前面。

关键的参数是MessageBoxOptions.DefaultDesktopOnly。注意,这将父消息框,默认桌面,导致应用程序调用MessageBox.Show()失去焦点。

(你真的应该保留这一行为的关键信息。)

另外,如果您的应用程序有一个窗口,显示通过调用MessageBox.Show()设置为this.BringToFront()第一个参数的消息框之前调用this。 (你会调用这个从窗口窗体类)。


2
投票

我已经回答了这个here(但因为它是一个相当小的答案,我会复制它):

using (var dummy = new Form() { TopMost = true })
{
    MessageBox.Show(dummy, text, title);
}

0
投票

你设置MessageBox主人尚未出一种新的形式。相反new Form(){TopMost=true, TopLevel=True}的,指的是你想在顶部的MessageBox现有表单的一个实例。


0
投票

继拉斯的回答,我有同样的问题和Lars'方法的工作,但如果我再由这不是在上面别处弹出一条消息,切换到它,然后用拉斯的方法再次调用的消息,这将不再是在上面。

这是我想出了变化很适合我,希望你能有所帮助:

这是从一个单独的线程中,具有到主应用程序的形式实例的引用运行

//Show message on top of all other forms
MainFormInstance.Invoke((MethodInvoker)delegate
{
    Form popup = new Form() { TopMost = true, TopLevel = true };
    MessageBox.Show(popup, "Message", "Title");
});

0
投票

试着写归纳逻辑: -

public static DialogResult ShowMessage(Form Parent, string Text, string Caption, MessageBoxButtons Buttons, MessageBoxIcon Icon, MessageBoxDefaultButton DefaultButton)
{
    if (Parent != null && Parent.InvokeRequired)
        return (DialogResult) Parent.Invoke(((Func<DialogResult>))(() => MessageBox.Show(Text, Caption, Buttons, Icon, DefaultButton)));
    else
        return (MessageBox.Show(Text, Caption, Buttons, Icon, DefaultButton));
}

0
投票

我一直在玩弄这一点,并在Windows 7 / 8.1 / 10运行多个测试,然后终于来到了一个工作方法在上面提到的所有系统显示在顶部的消息框。

//Create an Empty Form with TopMost & TopLevel attributes.
Form popup = new Form() { TopMost = true, TopLevel = true };

//Running MessageBox on a different Thread

              Invoke((MethodInvoker)delegate {

     DialogResult dialogResult = MessageBox.Show(popup, "Custom Message Here", MessageBoxButtons.YesNo);
              if (dialogResult == DialogResult.Yes)
              {

                  //do something
              }
              else if (dialogResult == DialogResult.No)
              {
                  //do something else
              }

//Or a casual message box

//MessageBox.Show(popup, "Custom Message Here", "Alert");

 });

-1
投票

只是做正常(MessageBox.Show(Message);)它已经最顶层。

看到herehere信息。

© www.soinside.com 2019 - 2024. All rights reserved.