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

后端 未结 7 1301
野性不改
野性不改 2021-01-06 02:03

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

相关标签:
7条回答
  • 2021-01-06 02:27

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

    0 讨论(0)
  • 2021-01-06 02:34

    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);
    
    0 讨论(0)
  • 2021-01-06 02:35
    https://stackoverflow.com/questions/29326042/show-message-box-in-net-console-application
    https://docs.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);
    
    0 讨论(0)
  • 2021-01-06 02:41

    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.

    0 讨论(0)
  • 2021-01-06 02:42

    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.

    0 讨论(0)
  • 2021-01-06 02:45

    I use this.

    MessageBox.Show(
                    "message",
                    "title",
                    MessageBoxButtons.OK,
                    messageBoxIcon,
                    MessageBoxDefaultButton.Button1,
                    (MessageBoxOptions)0x40000); // this set TopMost
    
    0 讨论(0)
提交回复
热议问题