Export (0) Print
Expand All

Walkthrough: Retrieving Dialog Box Information Collectively Using Objects 

Most Windows Forms dialog boxes have properties that expose information to the parent form. Rather than create multiple properties, you can expose a group of related data through a single dialog box property by creating a class object that stores all of the information needed by the parent form.

The following walkthrough creates a dialog box that exposes a UserInformation property containing name and e-mail address data, which is available to the parent form even after the dialog box is closed.

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

  1. In Visual Studio, create a new Windows Forms project called DialogBoxObjects. For more information, see How to: Create a Windows Application Project.

  2. Add a new Form to the project and name it InformationForm. For more information, see How to: Add Windows Forms to a Project.

  3. From the Toolbox, drag a TableLayoutPanel onto the form. Use the smart tag that appears as an arrow next to the control to add a third row to the table, and use the mouse to resize the rows so that all three are equal.

  4. Add a Label to each table cell in the first column, and a TextBox to each cell in the second column. The Label controls should be named, from top to bottom, firstNameLabel, lastNameLabel, and emailLabel; the TextBox controls should be named firstNameText, lastNameText, and emailText. When you are finished, add a Button control. Name it okButton, and change the Text property to OK.

  5. Add a new Class file named UserInformation to the project.

  6. Add the public qualifier to the class definition to make this class visible outside of its namespace, and add property definitions for the FirstName, LastName and EmailAddress properties. When you are finished, the code should look like this:

using System;
using System.Collections.Generic;
using System.Text;

namespace DialogBoxObjects
{
    public class UserInformation
    {
        private string _firstName = "";
        private string _lastName = "";
        private string _emailAddress = "";

        public string FirstName
        {
            get
            {
                return (_firstName);
            }
            set
            {
                _firstName = value;
            }
        }

        public string LastName
        {
            get
            {
                return (_lastName);
            }
            set
            {
                _lastName = value;
            }
        }

        public string EmailAddress
        {
            get
            {
                return (_emailAddress);
            }
            set
            {
                _emailAddress = value;
            }
        }
    }
}

  1. Go back to the code for InformationForm, and add a UserInformation property.

  1. Handle the Validated event on each of the TextBox controls so that you can update the corresponding property on UserInformation whenever one of the values change.

  1. Add handlers for the Validated event to each of the TextBox controls on your form, so that the new value of these controls is assigned to UserInformation whenever the user changes them.

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

  1. Select Form1 in Visual Studio. Add a Button to the form, and changes its Name property to showFormButton.

  2. Double-click the button to add an event handler that calls the dialog box and displays the results.

Community Additions

ADD
Show:
© 2014 Microsoft