In Visual Studio, create a new Windows Forms project called DialogBoxObjects. For more information, see How to: Create a Windows Application Project.
Add a new Form to the project and name it InformationForm. For more information, see How to: Add Windows Forms to a Project.
From the Toolbox, drag a TableLayoutPanel onto the InformationForm form.
Use the smart tag that appears as an arrow next to the TableLayoutPanel control to add a third row to the table.
Use the mouse to resize the rows so that all three rows are equal.
From the Toolbox, drag a Label to each table cell in the first column.
Set the Name property of the Label controls to firstNameLabel, lastNameLabel, and emailLabel.
Set the Text property of the controls to First Name, Last Name, and Email.
From the Toolbox, drag a TextBox to each cell in the second column.
Set the Name property of the TextBox controls to firstNameText, lastNameText, and emailText.
From the Toolbox, drag a Button control onto the form.
Set the Name property of the Button to closeFormButton.
Set the Text property of the Button to OK.
Add a new class file named UserInformation to the project.
For a C# project, add the public qualifier to the class definition to make this class visible outside of its namespace.
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
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;
}
}
}
}
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Text;
namespace DialogBoxObjects
{
public ref class UserInformation
{
public:
UserInformation()
{
firstNameValue = "";
lastNameValue = "";
emailAddressValue = "";
}
private:
String^ firstNameValue;
private:
String^ lastNameValue;
private:
String^ emailAddressValue;
public:
property String^ FirstName
{
String^ get()
{
return firstNameValue;
}
void set( String^ value )
{
firstNameValue = value;
}
}
public:
property String^ LastName
{
String^ get()
{
return lastNameValue;
}
void set( String^ value )
{
lastNameValue = value;
}
}
public:
property String^ EmailAddress
{
String^ get()
{
return emailAddressValue;
}
void set( String^ value )
{
emailAddressValue = value;
}
}
};
}
In Solution Explorer, right-click InformationForm and select View Code.
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
UserInformation _ui = new UserInformation();
public UserInformation UserInformation
{
get
{
return (_ui);
}
}
DialogBoxObjects::UserInformation^ userInformation;
public:
property DialogBoxObjects::UserInformation^ UserInformation
{
DialogBoxObjects::UserInformation^ get()
{
return this->userInformation;
}
}
In the InformationForm class, add a Validated event handler for each of the TextBox controls.
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
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;
}
}
private:
void FirstNameText_Validated(Object^ sender, EventArgs^ e)
{
String^ firstName = firstNameText->Text->Trim();
if (firstName->Length > 0)
{
userInformation->FirstName = firstName;
}
}
private:
void LastNameText_Validated(Object^ sender, EventArgs^ e)
{
String^ lastName = lastNameText->Text->Trim();
if (lastName->Length > 0)
{
userInformation->LastName = lastName;
}
}
private:
void EmailText_Validated(Object^ sender, EventArgs^ e)
{
String^ email = emailText->Text->Trim();
if (email->Length > 0)
{
userInformation->EmailAddress = email;
}
}
public:
InformationForm()
{
userInformation = gcnew DialogBoxObjects::UserInformation;
components = nullptr;
InitializeComponent();
firstNameText->Validated += gcnew EventHandler
(this, &InformationForm::FirstNameText_Validated);
lastNameText->Validated += gcnew EventHandler(this,
&InformationForm::LastNameText_Validated);
emailText->Validated += gcnew EventHandler(this,
&InformationForm::EmailText_Validated);
}
In the InformationForm class, add a Click event handler for the closeFormButton control.
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) Handles closeFormButton.Click
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
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();
}
private:
void CloseFormButton_Click(Object^ sender, EventArgs^ e)
{
if (userInformation->FirstName->Length == 0)
{
MessageBox::Show("First name cannot be zero-length.");
return;
}
if (userInformation->LastName->Length == 0)
{
MessageBox::Show("Last name cannot be zero-length.");
return;
}
if (userInformation->EmailAddress->Length == 0)
{
MessageBox::Show("Email address cannot be zero-length.");
return;
}
this->DialogResult = ::DialogResult::OK;
this->Hide();
}