Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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:

    Public Class UserInformation
        Private _FirstName As String = "" 
        Private _LastName As String = "" 
        Private _EmailAddress As String = "" 
    
        Public Property FirstName() As String 
            Get 
                Return _FirstName
            End Get 
            Set(ByVal value As String)
                _FirstName = value
            End Set 
        End Property 
    
    
        Public Property LastName() As String 
            Get 
                Return _LastName
            End Get 
            Set(ByVal value As String)
                _LastName = value
            End Set 
        End Property 
    
    
        Public Property EmailAddress() As String 
            Get 
                Return _EmailAddress
            End Get 
            Set(ByVal value As String)
                _EmailAddress = value
            End Set 
        End Property 
    End Class
    
  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.

    Private _UI As New UserInformation()
    
    Public ReadOnly Property UserInformation() As UserInformation
        Get 
            Return _UI
        End Get 
    End Property
    
  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.

    Private Sub FirstNameText_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles FirstNameText.Validated
        Dim FirstNameTemp As String = FirstNameText.Text.Trim()
        If FirstNameText.Text.Trim().Length > 0 Then
            _UI.FirstName = FirstNameTemp
        End If 
    End Sub 
    
    Private Sub LastNameText_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles LastNameText.Validated
        Dim LastNameTemp As String = LastNameText.Text.Trim()
        If LastNameText.Text.Trim().Length > 0 Then
            _UI.LastName = LastNameTemp
        End If 
    End Sub 
    
    Private Sub EmailText_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles EmailText.Validated
        Dim EmailTemp As String = EmailText.Text.Trim()
        If EmailTemp.Length > 0 Then
            _UI.EmailAddress = EmailTemp
        End If 
    End Sub
    
  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 Sub closeFormButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        If _UI.FirstName.Length = 0 Then
            MessageBox.Show("First name cannot be zero-length.")
            Exit Sub 
        End If 
        If _UI.LastName.Length = 0 Then
            MessageBox.Show("Last name cannot be zero-length.")
            Exit Sub 
        End If 
        If _UI.EmailAddress.Length = 0 Then
            MessageBox.Show("Email address cannot be zero-length.")
            Exit Sub 
        End If 
    
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
    

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 Sub ShowFormButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowFormButton.Click
        Dim InfoForm As New InformationForm()
        Dim dr As DialogResult = InfoForm.ShowDialog()
        If dr = System.Windows.Forms.DialogResult.OK Then 
            Dim Txt As String = "First Name: " & InfoForm.UserInformation.FirstName + ControlChars.Cr + ControlChars.Lf
            Txt &= "Last Name: " & InfoForm.UserInformation.LastName + ControlChars.Cr + ControlChars.Lf
            Txt &= "Email Address: " & InfoForm.UserInformation.EmailAddress
    
            MessageBox.Show(Txt)
        End If
    
        InfoForm.Dispose()
    End Sub
    
  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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.