Bagikan melalui


How to: Retrieve Dialog Box Information Selectively Using Multiple Properties

A common way to process the information returned from a dialog box is to provide a set of properties that return individual elements of dialog box data. This way, you can selectively extract data from the dialog box.

If you have dialog box data that is related, consider exposing some information through objects. For details, see Walkthrough: Retrieving Dialog Box Information Collectively Using Objects.

To expose dialog box information through properties

  1. In the class containing the code for your dialog box, create as many properties as needed to expose the necessary information for your dialog box. Provide a return value that is appropriate for the type of data you are providing.

  2. Add code to the Get portion of the property definition. If you want to prevent a user from changing the dialog box information outside of the dialog box, remove the Set portion of the property definition.

    The following code demonstrates how to expose the value of a combo box (cmbState) through a property defined in a dialog box:

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

Once you have exposed properties for all of the data you want to provide, you can retrieve the data from the application that uses the dialog box.

To retrieve data from the properties of a dialog box

  • In the form that is displaying the dialog box, open the event handler or method that you used to display the dialog box and determine its DialogResult. Add code to collect the properties of the dialog box's form as demonstrated in the following example:

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

See Also

Tasks

How to: Create Dialog Boxes at Design Time

How to: Close Dialog Boxes and Retain User Input

How to: Retrieve the Result for Dialog Boxes

Concepts

User Input to Dialog Boxes

Other Resources

Dialog Boxes in Windows Forms