共用方式為


HOW TO:使用多個屬性選擇性地擷取對話方塊資訊

更新:2007 年 11 月

處理對話方塊傳回資訊的常用方法,是提供一組屬性傳回對話方塊資料的個別項目。這樣,你可以選擇性地從對話方塊解壓縮資料。

假如你有關聯的對話方塊資料,請考慮經由物件顯露部分資訊。如需詳細資訊,請參閱逐步解說:使用物件擷取對話方塊整體資訊

若要經由屬性公開對話方塊資訊

  1. 在包含您對話方塊程式碼的類別內,建立您所需要的所有屬性,以公開對話方塊中必要的資訊。為提供的資料型別提供一個適當的傳回值。

  2. 在屬性定義中的 Get 部分加入程式碼。假如你要防止使用者在對話方塊外變更對話方塊資訊,請移除屬性定義中的 Set 部分。

    以下程式碼說明如何經由對話方塊中定義的屬性,公開下拉式方塊 (cmbState) 的值:

    Public Property StateSelected() As String
       Get
         Return cmbState.Text
       End Get
       Set(ByVal Value As String)
    
       End Set
    End Property
    
    public string StateSelected 
    {
       get
       {
          return cmbState.Text; 
       }
    }
    
    public String StateSelected()
    {   
       return comboBox1.get_SelectedText();
    }
    

一旦你公開所有要提供的資料屬性,你可以使用對話方塊從應用程式中擷取資料。

若要從對話方塊屬性中擷取資料

  • 在顯示對話方塊的表單中,開啟事件處理常式或用來顯示對話方塊的方法,並決定其 DialogResult 屬性。加入程式碼以收集對話方塊表單的屬性,如下列範例所示:

    Public Sub ShowMyDialog()
       ' Create and display an instance of the dialog box.
       Dim Dlg as New Form1()
       Dlg.ShowDialog()
    
       ' Determine the state of the DialogResult property for the form.
       If Dlg.DialogResult = DialogResult.OK Then
          ' Display the state that was selected in the dialog box's 
          ' combo box in a MessageBox.
          MessageBox.show Dlg.StateSelected
       End If
    End Sub
    
    private void ShowMyDialog() 
    {
       // Create and display an instance of the dialog box.
       Form1 dlg = new Form1();
       dlg.ShowDialog();
       // Determine the state of the DialogResult property for the form.
       if (dlg.DialogResult == DialogResult.OK) 
       {
          // Display the state that was selected in the dialog box's combo 
          // box in a MessageBox.
          MessageBox.Show (dlg.StateSelected);
       }
    }
    
    private void ShowMyDialog()
    {
       // Create and display an instance of the dialog box.
       Form1 dlg = new Form1();
       dlg.ShowDialog();
       // Determine the state of the DialogResult property for the form.
       if (dlg.get_DialogResult() == DialogResult.OK)
       {
          // Display the state that was selected in the dialog box's combo 
          // box in a MessageBox.
          MessageBox.Show(dlg.StateSelected());
       }
    }
    

請參閱

工作

HOW TO:在設計階段建立對話方塊

HOW TO:關閉對話方塊並保留使用者輸入

HOW TO:擷取對話方塊的結果

概念

使用者輸入到對話方塊

其他資源

Windows Form 中的對話方塊