Displaying a MessageBox on top of all forms, setti

2019-02-14 02:44发布

I have two forms and I set one of the forms' TopMost property to true. Somewhere, while the program runs, I show a MessageBox, but since TopMost is set to true, when the MessageBox pops up it shows under the topmost form so I cannot see it.

  1. Is there any way that I make one of my forms always be on top, but when a MessageBox pops up, make the message box show on top of that specific form?

  2. Is it possible to give a location to the MessageBox so that it shows not in the middle but for example low down on the screen?

  3. Let's say that I have an orange colored form can I have a pink colored message box only for that specific application. I mean I am NOT referring to playing the windows color properties. (I don't want all message boxes to be pink.)

6条回答
我只想做你的唯一
2楼-- · 2019-02-14 02:57

A simple approach for a top most MessageBox would be something like this:

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

You don't have to actually display the dummy form.

查看更多
姐就是有狂的资本
3楼-- · 2019-02-14 02:59

I think there is no built-in feature to do that in .Net, but I suggest you to keep a reference of your TopMost form, and change it before showing each message, something like following :

    public static void ShowMessage(string message)
    {
        Component.InstanceOfTopMost.TopMost = false;
        MessageBox.Show(message);
        Component.InstanceOfTopMost.TopMost = true;
    }

Component is a static class which is holds a reference of your form which should be TopMost. The reason of this static class is you may want to use that form in several places, this way you can easily access to your Form. This is a simple method, you can change it based on your requirements.

Update :

        public class Component
        {
            public static Form2 InstanceOfTopMost { get; set; }
        }

Component is just a name give another name to that, because there is another .Net class named Component.

        var frm = new Form2();
        Component.InstanceOfTopMost = frm;
        frm.Show();

Hope this help.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-02-14 03:05

@Saber Amani: why so? look, it just works:

    using System.Windows.Forms;

    namespace ReusingUserControlsSample
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                Form1 second = new Form1();
                second.TopMost = true;
                second.Show();

                MessageBox.Show(second, "BLARGH");
            }

            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                this.button1.Location = new System.Drawing.Point(178, 201);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(284, 264);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);

            }

            private System.Windows.Forms.Button button1;
        }
    }

The MSG is properly shown over the second form, which is TopMost. The only "problem" is to know which form is the topmost.

查看更多
该账号已被封号
5楼-- · 2019-02-14 03:06

I use this.

MessageBox.Show(
                "message",
                "title",
                MessageBoxButtons.OK,
                messageBoxIcon,
                MessageBoxDefaultButton.Button1,
                (MessageBoxOptions)0x40000); // this set TopMost
查看更多
贼婆χ
6楼-- · 2019-02-14 03:17

1) The MessageBox.Show method has an overload that takes a first parameter of Window type. If you use that overload instead of just Show(string), ie.:

class MyForm : Form {
    void method(){
       MessageBox.Show(this, "blablablablabla");
    }
}

then the MessageBox will show up in a 'modal' mode and it will be exactly on top on that form. Now just ensure that you pass that topmost form and you're done. Side effect is that the 'modal' mode will cause the Messagebox to BLOCK the original window until the message is dismissed.

2) No, that is not possible directly. However, you can play hard with .Net and get a "handle" to the messagebox and then move the window via P/Invoke to some WinApi functions, but I recommend you not.

3) No, that's just not possible with MessageBoxes

What you want to achieve in (2) and (3) is not possible, because the MsgBox is meant to be simple. To get that things you will have to write your own tiny form that will act as a message box, and present that form instead of the message box. That form will be able to have any styling, any position and any behaviour you like.

查看更多
爷、活的狠高调
7楼-- · 2019-02-14 03:18

To show a MessageBox on top of all the other forms of your application (including those with TopMost set) you can use the Show() method overload that takes a parameter of type MessageBoxOptions and pass MessageBoxOptions.ServiceNotification as that parameter.

DialogResult result = MessageBox.Show("Configuration file was corrupted.\n\nDo you want to reset it to default and lose all configurations?", "Config File Corrupted", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification);
查看更多
登录 后发表回答