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 New Windows Forms 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 InformationForm form.

  4. Use the smart tag that appears as an arrow next to the TableLayoutPanel control to add a third row to the table.

  5. Use the mouse to resize the rows so that all three rows are equal.

  6. From the Toolbox, drag a Label to each table cell in the first column.

  7. Set the Name property of the Label controls to firstNameLabel, lastNameLabel, and emailLabel.

  8. Set the Text property of the controls to First Name, Last Name, and Email.

  9. From the Toolbox, drag a TextBox to each cell in the second column.

  10. Set the Name property of the TextBox controls to firstNameText, lastNameText, and emailText.

  11. From the Toolbox, drag a Button control onto the form.

  12. Set the Name property of the Button to closeFormButton.

  13. Set the Text property of the Button to OK.

  14. Add a new class file named UserInformation to the project.

  15. For a C# project, add the public qualifier to the class definition to make this class visible outside of its namespace.

  16. In the UserInformation class, 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;
                }
            }
        }
    }
    
    
    
  17. In Solution Explorer, right-click InformationForm and select View Code.

  18. In the code-behind page for InformationForm, enter the following code in the InformationForm class to add a UserInformation property.

    
    UserInformation _ui = new UserInformation();
    
    public UserInformation UserInformation
    {
        get
        {
            return (_ui);
        }
    }
    
    
    
  19. In the InformationForm class, add a Validated event handler for each of the TextBox controls.

  20. In the event handler code, update the corresponding property of the UserInformation class whenever the TextBox value changes, as shown in the following code. For more information, see How to: Create Event Handlers Using the Designer.

    
    public InformationForm()
    {
        InitializeComponent();
    
        firstNameText.Validated += new EventHandler(firstNameText_Validated);
        lastNameText.Validated += new EventHandler(lastNameText_Validated);
        emailText.Validated += new EventHandler(emailText_Validated);
    }
    
    void firstNameText_Validated(object sender, EventArgs e)
    {
        string firstNameTemp = firstNameText.Text.Trim();
        if (firstNameText.Text.Trim().Length > 0)
        {
            _ui.FirstName = firstNameTemp;
        }
    }
    
    void lastNameText_Validated(object sender, EventArgs e)
    {
        string lastNameTemp = lastNameText.Text.Trim();
        if (lastNameText.Text.Trim().Length > 0)
        {
            _ui.LastName = lastNameTemp;
        }
    }
    
    void emailText_Validated(object sender, EventArgs e)
    {
        string emailTemp = emailText.Text.Trim();
        if (emailTemp.Length > 0)
        {
            _ui.EmailAddress = emailTemp;
        }
    }
    
    
    
  21. In the InformationForm class, add a Click event handler for the closeFormButton control.

  22. In the event handler code, validate the input and close the dialog box if the input is valid, as shown in the following code.

    
    private void closeFormButton_Click(object sender, EventArgs e)
    {
        if (_ui.FirstName.Length == 0)
        {
            MessageBox.Show("First name cannot be zero-length.");
            return;
        }
        if (_ui.LastName.Length == 0)
        {
            MessageBox.Show("Last name cannot be zero-length.");
            return;
        }
        if (_ui.EmailAddress.Length == 0)
        {
            MessageBox.Show("Email address cannot be zero-length.");
            return;
        }
    
        this.DialogResult = DialogResult.OK;
        this.Hide();
    }
    
    
    

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

  1. In Solution Explorer, right-click Form1 and select View Designer.

  2. From the Toolbox, drag a Button control onto the form.

  3. Set the Name property of the Button to showFormButton.

  4. Set the Text property of the Button to Show Form.

  5. Add a Click event handler for the showFormButton control.

  6. In the event handler code, call the dialog box and display the results, as shown in the following code.

    
    private void showFormButton_Click(object sender, EventArgs e)
    {
        InformationForm iForm = new InformationForm();
        DialogResult dr = iForm.ShowDialog();
        if (dr == DialogResult.OK)
        {
            string txt = "First Name: " + iForm.UserInformation.FirstName + "\r\n";
            txt += "Last Name: " + iForm.UserInformation.LastName + "\r\n";
            txt += "Email Address: " + iForm.UserInformation.EmailAddress;
    
            MessageBox.Show(txt);
        }
    
        iForm.Dispose();
    }
    
    
    
  7. Build and run the sample.

  8. When Form1 appears, click the Show Form button.

  9. When InformationForm appears, specify the information and then click OK.

    When you click OK, the information that you specified is retrieved from the form, the information is displayed in a message box, and then the form is closed.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft