Microsoft KB Archive/816145

= How to use the DialogResult property in Visual C# =

Article ID: 816145

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





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

IN THIS TASK

 * SUMMARY
 * Requirements
 * Using the DialogResult Property
 * Complete Code Listing
 * Verify That It Works



SUMMARY
This article demonstrates 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
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005

This article assumes that you are familiar with the following topics:
 * Windows applications
 * Microsoft Visual C#

back to the top

Using the DialogResult Property
This sample uses the DialogResult property to determine the button that you should click to close a form.  Create a new Windows Application project in Visual C# .NET or in Visual C# 2005. By default, Form1 is added to the project. In the Design window of Form1, add a Button control. Change the Name property to btnShowForm2, and then change the Text property to Show Form2 . On the Project menu, click Add Windows Form to add a new form to this project. You use this form as a dialog box. Click Open. A new form named Form2 is created.

Note In Visual Studio 2005, click Add.  Add two Button controls to Form2. Button1 and Button2 are added. Set the properties of these buttons as follows:

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

Button2
<ul> Name: btnCancel</li> DialogResult: Cancel</li> Text: Cancel</li></ul> </li> Form2 is complete. Notice that you have completed these steps by using the Windows Forms Designer and the Properties window. You did not copy or write any code.</li>  Return to the Design window of Form1. Double-click the btnShowForm2 control. The Visual Studio .NET IDE opens the Code window of Form1 and automatically creates the function prototype for the btnShowForm2_Click event: private void btnShowForm2_Click(object sender, System.EventArgs e) {

} </li>  Add the following code to the btnShowForm2_Click event procedure: DialogResult dr = new DialogResult ; Form2 frm2 = new Form2; dr = frm2.ShowDialog; if ( dr == DialogResult.OK ) MessageBox.Show (&quot;User clicked OK button&quot;); else if ( dr == DialogResult.Cancel) MessageBox.Show (&quot;User clicked Cancel button&quot;); </li></ol>

This displays Form2 as a modal dialog box. When you click OK or Cancel, a message box is displayed with the details of the button that the user clicked.

back to the top

Form1.cs
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;

namespace MyApplication {   ///     /// Summary description for Form1. ///    public class Form1 : System.Windows.Forms.Form {       private System.Windows.Forms.Button btnShowForm2; ///        /// Required designer variable. ///        private System.ComponentModel.Container components = null;

public Form1 {           //            // Required for Windows Form Designer support. //           InitializeComponent;

//           // TODO: Add any constructor code after InitializeComponent call. //       }

///        /// Clean up any resources being used. ///        protected override void Dispose( bool disposing ) {           if( disposing ) {               if (components != null) {                   components.Dispose; }           }            base.Dispose( disposing ); }

#region Windows Form Designer generated code ///        /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        private void InitializeComponent {           this.btnShowForm2 = new System.Windows.Forms.Button; this.SuspendLayout; //            // btnShowForm2 //            this.btnShowForm2.Location = new System.Drawing.Point(72, 40); this.btnShowForm2.Name = &quot;btnShowForm2&quot;; this.btnShowForm2.Size = new System.Drawing.Size(112, 23); this.btnShowForm2.TabIndex = 0; this.btnShowForm2.Text = &quot;Show Form2&quot;; this.btnShowForm2.Click += new System.EventHandler(this.btnShowForm2_Click); //            // Form1 //            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] {                                                                         this.btnShowForm2}); this.Name = &quot;Form1&quot;; this.Text = &quot;Form1&quot;; this.ResumeLayout(false);

}       #endregion

///        /// The main entry point for the application. ///        [STAThread] static void Main {           Application.Run(new Form1); }

private void btnShowForm2_Click(object sender, System.EventArgs e)       { DialogResult dr = new DialogResult ; Form2 frm2 = new Form2; dr = frm2.ShowDialog; if ( dr == DialogResult.OK ) MessageBox.Show (&quot;User clicked OK button&quot;); else if ( dr == DialogResult.Cancel) MessageBox.Show (&quot;User clicked Cancel button&quot;); }   } }

Form2.cs
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace MyApplication { ///    /// Summary description for Form2. ///    public class Form2 : System.Windows.Forms.Form {       private System.Windows.Forms.Button btnOK; private System.Windows.Forms.Button btnCancel; ///        /// Required designer variable. ///        private System.ComponentModel.Container components = null;

public Form2 {           //            // Required for Windows Form Designer support. //           InitializeComponent;

//           // TODO: Add any constructor code after InitializeComponent call //       }

///        /// Clean up any resources being used. ///        protected override void Dispose( bool disposing ) {           if( disposing ) {               if(components != null) {                   components.Dispose; }           }            base.Dispose( disposing ); }

#region Windows Form Designer generated code ///        /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        private void InitializeComponent {           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 = new System.Drawing.Point(40, 224); this.btnOK.Name = &quot;btnOK&quot;; this.btnOK.TabIndex = 0; this.btnOK.Text = &quot;OK&quot;; //            // btnCancel //            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.btnCancel.Location = new System.Drawing.Point(208, 224); this.btnCancel.Name = &quot;btnCancel&quot;; this.btnCancel.TabIndex = 1; this.btnCancel.Text = &quot;Cancel&quot;; //            // Form2 //            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.btnCancel); this.Controls.Add(this.btnOK); this.Name = &quot;Form2&quot;; this.Text = &quot;Form2&quot;; this.ResumeLayout(false);

}       #endregion } } Note The code is changed in Visual Studio 2005. When you create a Windows Forms project, Visual C# adds one form to the project by default. This form is named Form1. The two files that represent the form are named Form1.cs and Form1.designer.cs. You write your code in Form1.cs. The designer.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by dragging and dropping controls from the Toolbox. For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Web site:

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

Verify That It Works
<ol> Press CTRL+F5 to run the project. Form1 is displayed.</li> Click Show Form2.</li> To close the form, click either OK or Cancel. A message appears with the details of the button that you clicked. <ul> If you clicked OK, the following caption appears:

User clicked OK button

</li> If you clicked Cancel, the following caption appears:

User clicked Cancel button

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

back to the top

Keywords: kbprogramming kbproperties kbwindowsforms kbhowtomaster KB816145

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.