我有一部分绳索可以调出一个窗体。但不是每次都会在最前面,有时窗口消息框会在浏览器后面,我想知道有没有办法让它总是在最前面?我已经使用了 bringtofront,但它仍然无法正常工作。下面的代码是我调出窗体的代码。
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 顺序中的最高点。
我是一名优秀的程序员,十分优秀!