windows 的showDialog 方法

來源:互聯網
上載者:User
http://msdn2.microsoft.com/zh-cn/library/system.windows.forms.form.dialogresult(VS.80).aspx

 

表單的對話方塊結果是當表單顯示為強制回應對話方塊時從該表單返回的值。如果表單顯示為對話方塊,用 DialogResult 枚舉中的值設定此屬性將設定該表單的對話方塊結果值、隱形模式對話方塊並將控制返回給調用表單。此屬性通常由表單上 Button 控制項的 DialogResult 屬性設定。當使用者單擊 Button 控制項時,分配給 ButtonDialogResult 屬性的值將分配給該表單的 DialogResult 屬性。

當表單顯示為強制回應對話方塊時,單擊“關閉”按鈕(表單右上方帶 X 的按鈕)會隱藏表單並將 DialogResult 屬性設定為 DialogResult.Cancel。當使用者單擊對話方塊的“關閉”按鈕或設定 DialogResult 屬性的值時,不會自動調用 Close 方法。而是隱藏該表單並可重新顯示該表單,而不用建立該對話方塊的新執行個體。因為此行為,所以當應用程式不再需要該表單時,必須調用該表單的 Dispose 方法。

可以使用此屬性確定對話方塊是如何關閉的,以便正確處理在該對話方塊中執行的操作。

public void CreateMyForm()
 {
    // Create a new instance of the form.
    Form form1 = new Form();
    // Create two buttons to use as the accept and cancel buttons.
    Button button1 = new Button ();
    Button button2 = new Button ();
   
    // Set the text of button1 to "OK".
    button1.Text = "OK";
    // Set the position of the button on the form.
    button1.Location = new Point (10, 10);
    // 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.Height + button1.Top + 10);
    // Make button1's dialog result OK.
    button1.DialogResult = DialogResult.OK;
    // Make button2's dialog result Cancel.
    button2.DialogResult = 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 = 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 = 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.ShowDialog();
 
    // Determine if the OK button was clicked on the dialog box.
    if (form1.DialogResult == DialogResult.OK)
    {
       // Display a message box indicating that the OK button was clicked.
       MessageBox.Show("The OK button on the form was clicked.");
       // Optional: Call the Dispose method when you are finished with the dialog box.
       form1.Dispose();
    }
    else
    {
       // Display a message box indicating that the Cancel button was clicked.
       MessageBox.Show("The Cancel button on the form was clicked.");
       // Optional: Call the Dispose method when you are finished with the dialog box.
       form1.Dispose();
    }
 }
    

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.