HOW TO:擷取對話方塊的結果
一旦對話方塊關閉,顯示對話方塊的表單即可藉由參考其 DialogResult 屬性或是呼叫 ShowDialog 方法的傳回值,來擷取對話方塊的結果。 顯示對話方塊的表單接著可以按照傳回值來回應。
若要擷取 DialogResult 的值
將與下列相同的程式碼加入顯示對話方塊的方法。
基本上,這些程式碼是放在建立和顯示對話方塊的程式碼後面:
Public Sub DisplayDialog() ' Create and display an instance of the dialog box. Dim dlg as New Form() ' Show the dialog and determine the state of the ' DialogResult property for the form. If dlg.ShowDialog = DialogResult.OK Then ' Do something here to handle data from dialog box. End If End Sub
private void DisplayDialog() { // Create and display an instance of the dialog box Form dlg = new Form(); // Show the dialog and determine the state of the // DialogResult property for the form. if (dlg.ShowDialog() == DialogResult.OK ) { // Do something here to handle data from dialog box. } }
private void DisplayDialog() { // Create and display an instance of the dialog box Form dlg = new Form(); // Show the dialog and determine the state of the // DialogResult property for the form. if (dlg.ShowDialog() == DialogResult.OK ) { // Do something here to handle data from dialog box. } }
private: void DisplayDialog() { // Create and display an instance of the dialog box Form^ dlg = gcnew Form(); // Show the dialog and determine the state of the // DialogResult property for the form. if (dlg->ShowDialog() == DialogResult::OK ) { // Do something here to handle data from dialog box. } }
注意事項 重要的是,您必須呼叫表單上的 Dispose 方法,才能適當處置對話方塊。 如前所述,按一下 [關閉] 方塊或呼叫 Close 方法都無法自動進行這項作業。