窗口形式总是在顶部

时间:2015-09-06 16:00:26

标签: c# winforms messagebox

我有一部分电话线叫出窗口形式。但不是每次都会在顶部,有时候窗口消息框会在浏览器后面,我不知道有没有可以让它始终在顶部?我曾经使用过,但仍然无法使用。下面的代码是我调用窗口形式的代码。

public void CreateMyForm()
        { 
            // Create a new instance of the form.
            System.Windows.Forms.Form form1 = new System.Windows.Forms.Form();
            // Create two buttons to use as the accept and cancel buttons.
            System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
            System.Windows.Forms.Button button2 = new System.Windows.Forms.Button();
            System.Windows.Forms.Label lblMsg = new System.Windows.Forms.Label(); 

            form1.Size = new System.Drawing.Size(250,150);  

            lblMsg.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            lblMsg.ImageIndex = 1;
            lblMsg.ImageAlign = ContentAlignment.TopLeft;
            lblMsg.UseMnemonic = true;
            lblMsg.Text = "Are you sure? once save Status as CLOSED, this ticket detail cannot be change!";
            lblMsg.Size = new Size(lblMsg.PreferredWidth, lblMsg.PreferredHeight);
            form1.Text = "Are you sure? once save Status as CLOSED, this ticket detail cannot be change!";

            // Set the text of button1 to "OK".
            button1.Text = "OK";
            // Set the position of the button on the form.
            button1.Location = new Point(30, 70);
            // Set the text of button2 to "Cancel".
            button2.Text = "Cancel";
            // Set the position of the button based on the location of button1.
            button2.Location
               = new Point(button1.Left + button1.Width + 20, 70);
            // Make button1's dialog result OK.
            button1.DialogResult = System.Windows.Forms.DialogResult.OK;
            // Make button2's dialog result Cancel.
            button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            // Set the caption bar text of the form.   
            form1.Text = "My Dialog Box";

            // Define the border style of the form to a dialog box.
            form1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            // Set the accept button of the form to button1.
            form1.AcceptButton = button1;
            // Set the cancel button of the form to button2.
            form1.CancelButton = button2;
            // Set the start position of the form to the center of the screen.
            form1.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            // Add button1 to the form.
            form1.Controls.Add(button1);
            // Add button2 to the form.
            form1.Controls.Add(button2);

            // Display the form as a modal dialog box.
            form1.BringToFront();
            form1.ShowDialog();

            // Determine if the OK button was clicked on the dialog box. 
            if (form1.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                // Display a message box indicating that the OK button was clicked.
                update();
                // Optional: Call the Dispose method when you are finished with the dialog box.
                form1.Dispose();
            }
            else
            { 
                // Optional: Call the Dispose method when you are finished with the dialog box.
                form1.Dispose();
            }
        }

下面的代码也是一个显示erroe消息的消息框,但它也不总是位于顶部。

string errorMsg = "Error";
            if(DropDownListStatus.SelectedIndex == 4){
                System.Windows.Forms.MessageBox.Show(errorMsg, "window title", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);

            }

您的评论和建议很有用!谢谢

2 个答案:

答案 0 :(得分:0)

您只需将表单的TopMost属性设置为True

即可
        // Display the form as a modal dialog box.
        form1.TopMost = True;
        form1.ShowDialog();

最顶层的表单始终显示在桌面上Windows的z顺序的最高点。

答案 1 :(得分:0)

要使MessageBox.Show对话框最顶层,您可以执行以下操作

MessageBox.Show(new Form { TopMost = true }, "I am Top Most!", "title", 
    MessageBoxButtons.OK, 
    MessageBoxIcon.Warning);

显然,new Form { TopMost = true }将为消息框创建一个新的表单实例,并将TopMost设置为true。这将确保MessageBox显示在所有其他表单的前面,直到您关闭它。