6

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

Adam
  • 15,537
  • 2
  • 42
  • 63
modest and cute girl
  • 785
  • 4
  • 13
  • 24

7 Answers7

11

I use this.

MessageBox.Show(
                "message",
                "title",
                MessageBoxButtons.OK,
                messageBoxIcon,
                MessageBoxDefaultButton.Button1,
                (MessageBoxOptions)0x40000); // this set TopMost
Kim Ki Won
  • 1,795
  • 1
  • 22
  • 22
9

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.

zdimension
  • 977
  • 1
  • 12
  • 33
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • Read the question again, and test your code. It will not work in this case. – Saber Amani Aug 11 '12 at 00:16
  • thanks for informing and alternative way offering for question 2 and 3. i really thank you for that. and for part one i wrote – modest and cute girl Aug 11 '12 at 00:20
  • private void pictureBox1_Click(object sender, EventArgs e) { if (MessageBox.Show(this, "program will be closed you suere?", "!!!Warning!!!", MessageBoxButtons.YesNo) == DialogResult.Yes) { Application.Exit(); } } // but that is not working – modest and cute girl Aug 11 '12 at 00:21
  • by the way that close picture image (pictureBox1) is on the form1 and on above that form i have form2 which topmost propery set to true and when i click on the close pic the messagebox pops up but it goes under the form2 which is topmost – modest and cute girl Aug 11 '12 at 00:22
  • @quetzalcoatl the MessageBox 2th overloaded function solves the problem as quetzalcoatl stated. so i thank him alot for teaching and showing. so if i write MessageBox.Show(frm1, "BLARGH"); // then msg shows on top of frm1 no matter if its topmost set to true. if we write frm2 then it goes on top of frm2. again thanks to quetzalcoatl. – modest and cute girl Aug 14 '12 at 12:30
8

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);
Chris
  • 1,213
  • 1
  • 21
  • 38
5

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.

Joel
  • 7,401
  • 4
  • 52
  • 58
1

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.

Saber Amani
  • 6,409
  • 12
  • 53
  • 88
  • i combined your code and i had such a code but not worked. cuz not enough information about instanceof... i have the code below – modest and cute girl Aug 10 '12 at 23:51
  • private void pictureBox1_Click(object sender, EventArgs e) { Component.InstanceOfTopMost.TopMost = false; if (MessageBox.Show("program will be closed you suere?", "!!!Warning!!!", MessageBoxButtons.YesNo) == DialogResult.Yes) { Application.Exit(); } Component.InstanceOfTopMost.TopMost = true; } – modest and cute girl Aug 10 '12 at 23:51
  • What error or exception you got ? Did you populate the instance of your TopMost form ? – Saber Amani Aug 10 '12 at 23:54
  • that close picture (pictureBox1) is on the form1 and on above that form i have form2 which topmost propery set to true and when i click on the close pic the messagebox pops up but it goes under the form2 which is topmost – modest and cute girl Aug 10 '12 at 23:56
  • You didn't answer my question, did you populate the instance of Form2 in Component class ? – Saber Amani Aug 10 '12 at 23:58
  • IDE underlines the word InstanceOfTopMost with red and it says 'System.ComponentModel.Component' does not contain a definiton for 'InstanceOfTopMost' – modest and cute girl Aug 11 '12 at 00:02
  • i think after Component. there is no such a thing called InstanceOfTopMost – modest and cute girl Aug 11 '12 at 00:03
  • Sorry, there is another class named Component is one of .Net classes. Give a another name to your class. Also you have to declare that static class by yourself. – Saber Amani Aug 11 '12 at 00:05
  • I updated the post, Look the Component class name is just a name give it what do you like. Its not important, you have to declare a class which holds a instance of your TopMost Form. – Saber Amani Aug 11 '12 at 00:09
  • @saberAmani: I dont understand. Modal MSGBoxes just work. See my other post before I remove it as offtopic. – quetzalcoatl Aug 11 '12 at 00:30
  • @SaberAmani thanks for trying to help. quetzalcoatl's solution worked. to explain more: the MessageBox 2th overloaded function solves the problem as quetzalcoatl stated. so i thank him alot for teaching and showing. so if i write MessageBox.Show(frm1, "BLARGH"); // then msg shows on top of frm1 no matter if its topmost set to true. if we write frm2 then it goes on top of frm2. – modest and cute girl Aug 14 '12 at 12:31
1

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

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • guys its quite late in here 03:40 so i will go to sleep. i will check ur answers and try them and i will turn u back. but b4 i go i really want to say that i respect all ur knowledge and i envy. and im happy that people even make brain storming in here and try to proof. i hope i get that much expert in a very short time also. thank you Quetzalcoat and Saber for trying to help. – modest and cute girl Aug 11 '12 at 00:48
0
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);
Tom
  • 11
  • 1