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 topRequirements
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
- Text: OK
Button2
- Name: btnCancel
- DialogResult: Cancel
- Text: Cancel
- 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.
- 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)
{
}
- 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 ("User clicked OK button");
else if ( dr == DialogResult.Cancel)
MessageBox.Show ("User clicked Cancel button");
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 Complete Code Listing
Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MyApplication
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnShowForm2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support.
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call.
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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 = "btnShowForm2";
this.btnShowForm2.Size = new System.Drawing.Size(112, 23);
this.btnShowForm2.TabIndex = 0;
this.btnShowForm2.Text = "Show Form2";
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 = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[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 ("User clicked OK button");
else if ( dr == DialogResult.Cancel)
MessageBox.Show ("User clicked Cancel button");
}
}
}
Form2.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace MyApplication
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form2()
{
//
// Required for Windows Form Designer support.
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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 = "btnOK";
this.btnOK.TabIndex = 0;
this.btnOK.Text = "OK";
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(208, 224);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 1;
this.btnCancel.Text = "Cancel";
//
// 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 = "Form2";
this.Text = "Form2";
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:
Verify That It Works
- Press CTRL+F5 to run the
project. Form1 is displayed.
- Click Show Form2.
- To close the form, click either
OK or Cancel. A message appears with the details of the button that you clicked.
- If you clicked OK, the following caption appears:
- If you clicked
Cancel, the following caption appears:
User clicked Cancel button
back to the
top