SUMMARY
This step-by-step article describes how to use the
DialogResult property in Windows Forms. You can use the
DialogResult property to create dialog boxes in Windows
applications.
back to the topRequirements
This
article assumes that you are familiar with the following topics:
- Windows applications
- Microsoft Visual C++ .NET or Microsoft Visual C++ 2005
The
following list outlines the recommended hardware, software, network
infrastructure, and service packs that you need:
- Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005
back to the topUse the DialogResult Property
To create a sample that uses the
DialogResult property to determine the button that you clicked to close a
form, follow these steps:
- Start Microsoft Visual Studio .NET 2003 or Visual Studio 2005.
- On the File menu, point to
New, and then click Project.
- Click Visual
C++ Projects under Project Types, and then click Windows
Forms Application (.NET) under Templates.
Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows
Forms Application under Templates. - In the Name text box, type
Q816144.
- In the Location text box, type
C:\Test, and then click OK. By default,
Form1 is created.
- Open the Form1 form in Design view.
- Add a Button control to Form1,
and then press F4 to view the Properties pane.
- Change the Name property to
btnShowForm2, and then change the Text
property to Show Form2.
- On the Project menu, click Add New
Item.
- Under
Templates in the Add New Item dialog box, click Windows Form
(.NET).
Note In Visual Studio 2005, click Windows Form. - In the Name text box, type
Form2, and then click Open. A new form
that is named Form2 is created. You use this new form as a dialog
box.
Note In Visual Studio 2005, click Add. - Add two Button controls to Form2.
By default, Button1 and Button2 are added.
- Press F4 to view the Properties
pane, and then set the properties of the buttons that you added as follows:
Button1- Name:
btnOK
- DialogResult:
OK
- Text:
OK
Button2- Name: btnCancel
- DialogResult: Cancel
- Text: Cancel
Designing the Form2 form is complete. Notice that you have
completed these steps by using the Windows Forms Designer and the Properties
pane. You did not copy or write any code. - Open Form1 in Design view.
- Double-click Show Form2. Visual Studio
.NET or Visual Studio 2005 open the Code window of Form1 and automatically creates the
following method prototype for the btnShowForm2_Click event:
private: System::Void btnShowForm2_Click(System::Object * sender, System::EventArgs * e)
{
}
- Add the following code to the
btnShowForm2_Click event method:
System::Windows::Forms::DialogResult dr;
Form2 *newDlg = new Form2();
dr = newDlg->ShowDialog();
if ( dr == DialogResult::OK )
MessageBox::Show ("User clicked the OK button");
else if ( dr == DialogResult::Cancel)
MessageBox::Show ("User clicked the Cancel button");
- Add the following code at the top of the Form1.h file to
avoid compiler errors:
#include "Form2.h"
- Press the CTRL+Shift+B key combination to compile the
application.
back to the topComplete Code Listing
Form1.h
#pragma once
#include "Form2.h"
namespace Q816144
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Button * btnShowForm2;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->btnShowForm2 = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// btnShowForm2
//
this->btnShowForm2->Location = System::Drawing::Point(64, 96);
this->btnShowForm2->Name = S"btnShowForm2";
this->btnShowForm2->Size = System::Drawing::Size(72, 23);
this->btnShowForm2->TabIndex = 0;
this->btnShowForm2->Text = S"Show Form2";
this->btnShowForm2->Click += new System::EventHandler(this, btnShowForm2_Click);
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->btnShowForm2);
this->Name = S"Form1";
this->Text = S"Form1";
this->ResumeLayout(false);
}
private: System::Void btnShowForm2_Click(System::Object * sender, System::EventArgs * e)
{
System::Windows::Forms::DialogResult dr;
Form2 *newDlg = new Form2();
dr = newDlg->ShowDialog();
if ( dr == DialogResult::OK )
MessageBox::Show ("User clicked the OK button");
else if ( dr == DialogResult::Cancel)
MessageBox::Show ("User clicked the Cancel button");
}
};
}
Note You must add the common language runtime support compiler option (
/clr:oldSyntax) in Visual C++ 2005 to successfully compile this code sample.
To do this, follow these steps:
- Click Project, and then click ProjectName Properties.
Note ProjectName represents the name of the project. - Expand Configuration Properties, and then click General.
- Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting on the right pane, click Apply, and then click OK.
For more information about the common language runtime support compiler options, visit the following Microsoft Web site:
Form2.h
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace My
{
public __gc class Form2 : public System::Windows::Forms::Form
{
public:
Form2(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Button * btnOK;
private: System::Windows::Forms::Button * btnCancel;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container* components;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->btnOK = new System::Windows::Forms::Button();
this->btnCancel = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// btnOK
//
this->btnOK->DialogResult = System::Windows::Forms::DialogResult::OK;
this->btnOK->Location = System::Drawing::Point(24, 128);
this->btnOK->Name = S"btnOK";
this->btnOK->TabIndex = 0;
this->btnOK->Text = S"OK";
//
// btnCancel
//
this->btnCancel->DialogResult = System::Windows::Forms::DialogResult::Cancel;
this->btnCancel->Location = System::Drawing::Point(176, 128);
this->btnCancel->Name = S"btnCancel";
this->btnCancel->TabIndex = 1;
this->btnCancel->Text = S"Cancel";
//
// Form2
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->btnCancel);
this->Controls->Add(this->btnOK);
this->Name = S"Form2";
this->Text = S"Form2";
this->ResumeLayout(false);
}
};
}
back to the topVerify That Your Application Works
- Press the CTRL+F5 key combination to run the
application. Form1 appears.
- Click Show Form2. Form2 appears.
- To close Form2, click OK
or click Cancel. You receive a message box that contains the details
of the button that you clicked.
- If you click OK, you receive a message
box that contains the following text:User clicked the
OK button
- If you click Cancel, you receive a
message box that contains the following text:User
clicked the Cancel button
back to the
top