2015-09-06 141 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顯示在所有其他表單之前,直到您關閉它。