Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Walkthrough: Retrieving Dialog Box Information Collectively Using Objects

Visual Studio .NET 2003

Most dialog boxes have properties that expose information provided within the dialog box. You might need to expose some or all of the information, depending on the amount of it the dialog box needs to convey to its parent form.

You can expose a group of related data from a dialog box by creating a class object that stores all of a dialog box's information. With this object created and defined, you can return all of the dialog box's data through a single property.

Before you can retrieve dialog box information, you need to create the class object that will disclose the dialog box information and expose it using a property in your dialog box class.

In the following example, a dialog box is created that exposes a property that holds geographical coordinate information. The information entered into the dialog box is then available via an object after the dialog box is closed.

To create a dialog box that will expose its data through an object

  1. Create a Windows Application called "MapDialog". For details, see Creating a Windows Application Project.
  2. Rename the form "MapDialog" by right-clicking the form in Solution Explorer and choosing Rename. This will be the dialog box where coordinate information is entered.
    Note   Skip this step for Visual C++ projects.
  3. Drag two TextBox controls from the Toolbox.

    Set the Name and Text properties of one of the Textbox controls to Latitude and the Name and Text properties of the other to Longitude in the Properties window.

  4. Drag two Button controls from the Toolbox.

    Rename the first Button control OK and set the DialogResult property to OK. Rename the second Button control Cancel, and set the DialogResult property to Cancel.

  5. Create an event handler for each of the Button controls' Click events that closes the form. For details, see Creating Event Handlers on the Windows Forms Designer.
    ' Visual Basic
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
       Me.Close()
    End Sub
    
    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
       Me.Close()
    End Sub
    // C#
    private void OK_Click(object sender, System.EventArgs e)
    {
       this.Close();
    }
    
    private void Cancel_Click(object sender, System.EventArgs e)
    {
       this.Close();
    }
    // C++
    private:
      System::Void OK_Click(System::Object *  sender,
        System::EventArgs *  e)
      {
        this->Close();
      }
    
    private:
      System::Void Cancel_Click(System::Object *  sender,
        System::EventArgs *  e)
      {
        this->Close();
      }
    
  6. Find where the class is declared.
    ' Visual Basic
    Public Class Form1
    // C#
    public class Form1 : System.Windows.Forms.Form
    // C++
    public __gc class Form1 : public Form
    

    Change the name of the class to MapDialog.

    ' Visual Basic
    Public Class MapDialog
    // C#
    public class MapDialog : System.Windows.Forms.Form
    // C++
    public __gc class MapDialog : public Form
    
  7. If you are using Visual C# or Visual C++:
    1. Find the constructor, which looks like this:
      // C#
      public Form1()
      // C++
      public:
        Form1(void)
      

      Change the name in the constructor to MapDialog.

      // C#
      public MapDialog()
      // C++
      public:
        MapDialog(void)
      
    2. Remove the Main (in Visual C#) or WinMain (in Visual C++) function, which looks like this:
      // C#
      [STA Thread]
      static void Main()
      {
      Application.Run(new Form1());
      }
      
      // C++
      [System::STAThreadAttribute]
      int __stdcall WinMain()
      {
        Application::Run(new Form1());
      }
      
      Note   The
      WinMain
      function is located in Form1.cpp.

      You will be adding a Main or WinMain procedure to another form that you create below.

  8. Create a new class called MapCoordinates.
    Note   You can continue to design the class
    MapDialog
    in the Windows Forms Designer as long as it remains as the first class in the file. However, in C++, you must define the
    MapCoordinates
    class before the
    MapDialog
    class because in the next step, you will create a property in the
    MapDialog
    class that references the
    MapCoordinates
    object.

    Within the class, instantiate two Integers to represent the latitude and longitude of a set of coordinates.

    ' Visual Basic
    Public Class MapCoordinates
       Public Latitude As Integer
       Public Longitude As Integer
    End Class
    
    // C#
    public class MapCoordinates
    {
       public int Latitude;
       public int Longitude;
    }
    
    // C++
    public __gc class MapCoordinates
    {
      public: int Latitude;
      public: int Longitude;
    };
    
  9. In the MapDialog class, create a property that you will use to hold the dialog box's data.

    The following code is an example of how this can be done:

    ' Visual Basic
    Public ReadOnly Property MapCoordinates() As MapCoordinates
       ' Create an instance of your object. In this case, create a map
       ' coodinates object that stores map latitude and longitude
       ' coordinates.
       Get
          Dim mc As New MapCoordinates()
          ' Note that the text boxes must have a numeric value in them.
          mc.Latitude = Me.Latitude.Text
          mc.Longitude = Me.Longitude.Text
          Return mc
       End Get
    End Property
    
    // C#
    public MapCoordinates MapCoordinates 
    {
       get 
       {
          // Create an instance of your object. In this case, create a map 
          // coordinates object that stores map latitude and longitude coordinates.
          MapCoordinates mc = new MapCoordinates();
          mc.Latitude =  System.Convert.ToInt32(this.Latitude.Text);
          mc.Longitude = System.Convert.ToInt32(this.Longitude.Text);
          return mc;
       }
    }
    
    // C++
      public:
        __property MapDialog1::MapCoordinates *get_MapCoordinates()
        {
          MapDialog1::MapCoordinates *mc =
            new MapDialog1::MapCoordinates();
          mc->Latitude = System::Convert::ToInt32(this->Latitude->Text);
          mc->Longitude = System::Convert::ToInt32(this->Longitude->Text);
          return mc;
        }
    

You can retrieve the dialog-box data through the parent form by accessing the property you created and storing the data in an object.

To show the dialog you created and retrieve the data using an object

  1. Add a form to your project, and name it Form2. For details, see Adding Windows Forms to a Project.
  2. If you are using Visual C# or Visual C++, add a Main or WinMain procedure to this new form. This addition allows Form2 to function as a startup object.

    The following code is an example of how this can be done using a form named Form2:

    // C#
    static void Main()
    {
       Application.Run(new Form2());
    }
    
    // C++
    [System::STAThreadAttribute]
    int __stdcall WinMain()
    {
      Application::Run(new Form2());
    }
    

    In Visual C++, add the following #include directive at the top of Form2.h:

    // C++
    #include "Form1.h"
    
  3. In a procedure, do the following:
    • Display an instance of the dialog box and create a reference variable that references the data entered by the user into the dialog box.
    • Add code to assign the value of the property that contains the dialog box information to your reference variable. Add this code after the code that determines the result of the dialog box.
    • Use the data provided by the object in your application.

    The following code demonstrates accessing the data from the MapCoordinates object:

    ' Visual Basic
    Public Sub DisplayMapDialogBox()
       ' Create and display an instance of the dialog box.
       Dim dlg as new MapDialog()
       dlg.ShowDialog()
       ' Determine the state of the DialogResult property for the form.
       If dlg.DialogResult = DialogResult.OK Then
          ' Create an instance of the MapCoordinates object to store the 
          ' dialog data.
          Dim mc as MapCoordinates
          mc = dlg.MapCoordinates
          ' Display the values returned from the dialog box's return 
          ' object.
          MessageBox.Show(mc.Latitude.ToString())
          MessageBox.Show(mc.Longitude.ToString())
       End If
    End Sub
    
    // C#
    public void DisplayMapDialogBox() 
    {
       // Create and display an instance of the dialog box.
       MapDialog dlg = new MapDialog();
       dlg.ShowDialog();
       // Determine the state of the DialogResult property for the form.
       if (dlg.DialogResult == DialogResult.OK) 
       {
          // Create an instance of the MapCoordinates object to store the 
          // dialog data.
          MapCoordinates mc = dlg.MapCoordinates;
          // Display the values returned from the dialog box's return 
          // object.
         MessageBox.Show (mc.Latitude.ToString());
         MessageBox.Show (mc.Longitude.ToString());
       }
    }
    
    // C++
      public:
        System::Void DisplayMapDialogBox() 
        {
          // Create and display an instance of the dialog box.
          MapDialog *dlg = new MapDialog;
          dlg->ShowDialog();
          // Determine the state of the DialogResult property for the form.
          if (dlg->DialogResult == DialogResult::OK) 
          {
            // Create an instance of the MapCoordinates object to store the 
            // dialog data.
            MapCoordinates *mc = new MapCoordinates();
            mc = dlg->MapCoordinates;
            // Display the values returned from the dialog box's return 
            // object.
            MessageBox::Show (mc->Latitude.ToString());
            MessageBox::Show (mc->Longitude.ToString());
          }
        }
    
  4. Add a line of code to the constructor of your second form (the startup form), to call the procedure that you just created.
    ' Visual Basic
    DisplayMapDialogBox()
    
    // C#
    DisplayMapDialogBox();
    
    // C#
    DisplayMapDialogBox();
    
  5. In Solution Explorer, right-click the MapDialog project and choose Properties. In the property page that opens, set the Startup Object to the second form you added.
  6. Press F5 to run the project. The second form will open an instance of the first form. If you supply latitude and longitude values in the appropriate text boxes and press the OK button, the second form retrieves these property values from the first form and displays them in message boxes.

See Also

Dialog Boxes in Windows Forms | User Input to Dialog Boxes | Creating Dialog Boxes | Closing Dialog Boxes and Retaining User Input | Retrieving Dialog Box Information Selectively Using Multiple Properties | Creating Windows Forms

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft