Microsoft KB Archive/816144

= How to use the DialogResult property in Visual C++ .NET and in Visual C++ 2005 =

Article ID: 816144

Article Last Modified on 11/22/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition

-







For a Microsoft Visual Basic .NET version of this article, see 315711.



For a Microsoft Visual C# .NET version of this article, see 816145.

This article references the following Microsoft .NET Framework Class Library namespaces:
 * System::Windows::Forms
 * System::Collections
 * System::ComponentModel

IN THIS TASK

 * SUMMARY
 * Requirements
 * Use the DialogResult Property
 * Complete Code Listing
 * Verify That Your Application Works
 * REFERENCES



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 top

Requirements
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 top

Use 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.</li> Add a Button control to Form1, and then press F4 to view the Properties pane.</li> Change the Name property to btnShowForm2, and then change the Text property to Show Form2 .</li> On the Project menu, click Add New Item.</li> Under Templates in the Add New Item dialog box, click Windows Form (.NET).

Note In Visual Studio 2005, click Windows Form.</li> 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.</li> Add two Button controls to Form2. By default, Button1 and Button2 are added.</li> Press F4 to view the Properties pane, and then set the properties of the buttons that you added as follows:

Button1 <ul> Name: btnOK </li> DialogResult: OK </li> Text: OK </li></ul>

Button2 <ul> Name: btnCancel </li> DialogResult: Cancel </li> Text: Cancel </li></ul>

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.</li> Open Form1 in Design view.</li>  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) { } </li> <li> 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 (&quot;User clicked the OK button&quot;); else if ( dr == DialogResult::Cancel) MessageBox::Show (&quot;User clicked the Cancel button&quot;); </li> <li> Add the following code at the top of the Form1.h file to avoid compiler errors: <li>Press the CTRL+Shift+B key combination to compile the application.</li></ol>
 * 1) include &quot;Form2.h&quot; </li>

back to the top

Form1.h

 * 1) pragma once
 * 2) include &quot;Form2.h&quot;

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: ///        /// Required designer variable. ///        System::ComponentModel::Container * components;

///        /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        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&quot;btnShowForm2&quot;; this->btnShowForm2->Size = System::Drawing::Size(72, 23); this->btnShowForm2->TabIndex = 0; this->btnShowForm2->Text = S&quot;Show Form2&quot;; 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&quot;Form1&quot;; this->Text = S&quot;Form1&quot;; 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 (&quot;User clicked the OK button&quot;); else if ( dr == DialogResult::Cancel) MessageBox::Show (&quot;User clicked the Cancel button&quot;);

}

}; } 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:
 * 1) Click Project, and then click ProjectName Properties.

Note ProjectName represents the name of the project.
 * 1) Expand Configuration Properties, and then click General.
 * 2) 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:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

Form2.h

 * 1) 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: ///        /// Required designer variable. ///        System::ComponentModel::Container* components;

///        /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        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&quot;btnOK&quot;; this->btnOK->TabIndex = 0; this->btnOK->Text = S&quot;OK&quot;; //            // btnCancel //            this->btnCancel->DialogResult = System::Windows::Forms::DialogResult::Cancel; this->btnCancel->Location = System::Drawing::Point(176, 128); this->btnCancel->Name = S&quot;btnCancel&quot;; this->btnCancel->TabIndex = 1; this->btnCancel->Text = S&quot;Cancel&quot;; //            // 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&quot;Form2&quot;; this->Text = S&quot;Form2&quot;; this->ResumeLayout(false);

}          }; } back to the top

Verify That Your Application Works
<ol> <li>Press the CTRL+F5 key combination to run the application. Form1 appears.</li> <li>Click Show Form2. Form2 appears.</li> <li>To close Form2, click OK or click Cancel. You receive a message box that contains the details of the button that you clicked. <ul> <li>If you click OK, you receive a message box that contains the following text:

User clicked the OK button

</li> <li>If you click Cancel, you receive a message box that contains the following text:

User clicked the Cancel button

</li></ul> </li></ol>

back to the top

<div class="references_section">