2015-09-06 103 views
1

我有一个窗口窗口呼叫的一部分。但不是每次都会在最上面,有时窗口消息框会在浏览器后面,不知有没有可以让它始终处于顶端的?我已经使用过,但它仍然不起作用。下面的代码是我的代码,调出窗口窗体。总是在窗口顶部的窗口

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

      } 

您的意见和建议是多appreaciated!..谢谢你

+0

我无法明白为什么当你可以刚刚使用'MessageBox.Show()'和appriopiate参数时,为什么要编程创建你自己的“OK/Cancel”对话框。另外,你是否将新创建的窗口的父窗口设置为正确?这可能是为什么'BringToFront()'没有效果。否则,你仍然可以通过将窗体的'TopMost'属性设置为'true'来强制窗口总是**。 (https://msdn.microsoft.com/en-us/library/system.windows.forms.form.topmost.aspx) –

+0

之所以这样是因为我的系统需要支持多语言,这就是为什么我需要创建dialoge。希望你能理解它。 – Handsome

+1

为什么你把它标记为ASP.NET? – mason

回答

0

你只需要形式的TopMost属性设置为True

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

朵蒙特形式在始终显示桌面上的窗口的z顺序中的最高点。

+0

我可以让它像用户需要单击窗口窗体上的按钮吗? – Handsome

+0

是的,只需在按钮事件处理程序中将TopMost设置为True ... – aleroot

0

为了使MessageBox.Show对话框最上面的,你可以这样做以下

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

显然,new Form { TopMost = true }将与最顶层设置为true,这些URL创建一个新的表单实例。这将确保MessageBox显示在所有其他表单之前,直到您关闭它。