How to use the DialogResult property in Visual C++ .NET and in Visual C++ 2005 (816144)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2003)
  • 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

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:
  1. Start Microsoft Visual Studio .NET 2003 or Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. In the Name text box, type Q816144.
  5. In the Location text box, type C:\Test, and then click OK. By default, Form1 is created.
  6. Open the Form1 form in Design view.
  7. Add a Button control to Form1, and then press F4 to view the Properties pane.
  8. Change the Name property to btnShowForm2, and then change the Text property to Show Form2.
  9. On the Project menu, click Add New Item.
  10. Under Templates in the Add New Item dialog box, click Windows Form (.NET).

    Note In Visual Studio 2005, click Windows Form.
  11. 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.
  12. Add two Button controls to Form2. By default, Button1 and Button2 are added.
  13. 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.
  14. Open Form1 in Design view.
  15. 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)
    {
    }
  16. 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");
    
  17. Add the following code at the top of the Form1.h file to avoid compiler errors:
    #include "Form2.h"
  18. Press the CTRL+Shift+B key combination to compile the application.
back to the top

Complete 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:
  1. Click Project, and then click ProjectName Properties.

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

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

Verify That Your Application Works

  1. Press the CTRL+F5 key combination to run the application. Form1 appears.
  2. Click Show Form2. Form2 appears.
  3. 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

REFERENCES

For more information about the Button.DialogResult property, visit the following Microsoft Developer Network (MSDN) Web site:back to the top

Modification Type:MajorLast Reviewed:1/12/2006
Keywords:kbWindowsForms kbDlg kbcode kbProperties kbProgramming kbHOWTOmaster KB816144 kbAudDeveloper kbAudITPRO