Display MessageBox on top of all forms, adjust location and / or color

I have two forms, and I set true for one of the properties of the form TopMost. Somewhere during the execution of the program, I show a MessageBox, but since TopMost is set to true when the MessageBox appears, it is displayed under the topmost form, so I do not see it.

  • Is there a way that I do one of my forms to always be at the top, but when a MessageBox appears, create a message box on top of that particular form?

  • Is it possible to specify a location in MessageBox so that it does not appear in the middle, but, for example, at the bottom of the screen?

  • Let's say that I have an orange shape, I have a window with a color image just for this particular application. I mean, I do NOT mean reproducing the color properties of Windows. (I do not want all message boxes to be pink.)

+4
source share
6 answers

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

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

MessageBox "", . , , . , "" Messagebox , .

2) , . .Net "" , P/Invoke WinApi, .

3) , MessageBoxes

, (2) (3), , MsgBox . , , , . , , .

+6

.

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

MessageBox :

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

.

+5

MessageBox ( TopMost), Show() , MessageBoxOptions MessageBoxOptions.ServiceNotification .

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

@ : ? , :

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

MSG , TopMost. "" , .

+1

, .NET , TopMost , , :

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

Component - , , TopMost. , , . , .

:

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

A component is simply a name that is given a different name because there is another .Net class called Component.

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

I hope for this help.

0
source

All Articles