modest and cute girl
modest and cute girl

Reputation: 785

Displaying a MessageBox on top of all forms, setting location and/or color

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.)

Upvotes: 6

Views: 40648

Answers (7)

Tom
Tom

Reputation: 11

https://stackoverflow.com/questions/29326042/show-message-box-in-net-console-application
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox

using System.Runtime.InteropServices;
//...
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr h, string m, string c, uint type);
const uint ICONERROR = 16;
const uint ICONWARNING = 48;
const uint ICONINFORMATION = 64;
const uint MB_TOPMOST = 262144;

//...
MessageBox((IntPtr)0, "Started" + DateTime.Now, "Log", ICONINFORMATION | MB_TOPMOST);

Upvotes: 0

quetzalcoatl
quetzalcoatl

Reputation: 33506

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.

Upvotes: 9

Kim Ki Won
Kim Ki Won

Reputation: 1855

I use this.

MessageBox.Show(
                "message",
                "title",
                MessageBoxButtons.OK,
                messageBoxIcon,
                MessageBoxDefaultButton.Button1,
                (MessageBoxOptions)0x40000); // this set TopMost

Upvotes: 11

Chris
Chris

Reputation: 1283

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

Upvotes: 9

Joel
Joel

Reputation: 7569

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.

Upvotes: 5

quetzalcoatl
quetzalcoatl

Reputation: 33506

@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.

Upvotes: 1

Saber Amani
Saber Amani

Reputation: 6489

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.

Upvotes: 1

Related Questions