窗体总是在顶部
本文关键字:顶部 窗体 | 更新日期: 2023-09-27 18:11:21
我有一个调用窗口窗体的部分。但不是每次都会在顶部,有时窗口消息框会在浏览器后面,我想知道有什么方法可以使它总是在顶部?我用了"前置",但还是不行。下面的代码是我调用窗口窗体的代码。
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();
}
}
下面的代码也是一个显示错误消息的消息框,但它也不总是在顶部。
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);
}
非常感谢您的评论和建议!谢谢你
你只需要将表单的TopMost属性设置为True
// Display the form as a modal dialog box.
form1.TopMost = True;
form1.ShowDialog();
最顶层的窗体总是显示在桌面上窗口z轴顺序的最高点。
要使MessageBox.Show
对话框位于最上面,您可以执行以下操作
MessageBox.Show(new Form { TopMost = true }, "I am Top Most!", "title",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
显然,new Form { TopMost = true }
将为消息框创建一个新的表单实例,并将TopMost设置为true。这将确保MessageBox显示在所有其他表单的前面,直到您关闭它。