Passing Data Between a Windows Form and Dialog Boxes


Introduction

In this example, we will create a windows form application to demonstrate the transfer of data between a windows form and a dialog box displayed from the windows form.

Details

Our windows application will display a windows form which has a button to prompt the user for his/her name and a label to display a greeting to the user. The windows form also contains a combo-box which allows the user to select his/her favorite color.

When the user clicks on the "Enter Name"button, we display another Dialog Box which will accept the user name as input. If the user has selected a color in the first windows form, we display the dialog box with the backcolor as selected by the user. When the user clicks OK in the dialog box, we display a greeting to the user in Label1 in the Main form, using the data entered in the dialog box and if the user clicks on Cancel in the dialog box, we display a greeting to the guest, in the Main form.

This example may seem a little contrived as it is designed to demonstrate the techniques.

Step 1 : Create the windows application

Create a new Visual C# Windows Forms application. For users not using Visual Studio.Net, the complete source code is available at the end of the article.

Step 2 : Design the Parent Form

Add the controls on the default windows form (Form1) as shown in the figure below.



Figure 1: Main Form-User interface

Add the following items in the ComboBox Items collection:
Red
Green
Blue
Yellow

Set the Modifiers property of the ComboBox to Public. In the Form_Load event, set the first item in the Combo Box as the default selection.

private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.SelectedIndex =0;
}

Step 3 : Design the Dialog Box

We will now move on to the design of the dialog box and take a look at the technique for accessing data from the main form from the dialog box. We will also make available the User Name input by the user, so that it can be accessed by the main form.

Add a new Windows Form in the application (Form2). Add controls on the new form as shown below:



Figure 2: Dialog Box

In this form, set the DialogResult property of the OK button to "OK" and to "Cancel" for the Cancel button.

Define the following properties /fields in the Form2 form. The MyParentForm property will be set to an instance of the Parent form of the application. This property will be used to access data from the Parent Form. If the Parent form was a MDI parent and this form was it's child, we could have accessed the Parent Form from the ParentForm property of the MDI Child form.

The user name property gives access to the data entered by the user in the text box in the dialog. This property will be used by the Parent Form to access data from the dialog box.

public Form1 MyParentForm;
public string UserName
{
get{return txtname.Text;}
}

In the Click event handlers for both the buttons , add code to close the dialog box.

private void button1_Click(object sender, System.EventArgs e)
{
Close();
}

private void button2_Click(object sender, System.EventArgs e)
{
Close();
}

Step 4 : Access Parent Form data from the dialog box

In the Form Load event of the dialog box, we will access the value selected by the user in the combo box on the parent form and use that color value as the backcolor for the form.

private void Form2_Load(object sender, System.EventArgs e)
{
Color clr = Color.FromName(((Form1)MyParentForm).comboBox1.SelectedItem.ToString());
this.BackColor = clr;
}

We will see in the next step how we have set the "MyParentForm" field to point to the parent form, before we displayed the dialog box.

Step 5 : Access Dialog Box data from the Parent Form

Our dialog box is displayed when the Button is clicked in the parent form.

After the dialog box is closed, we return back to next step in the click event handler after the parent form was invoked. The ShowDialog method returns DialogResult.OK or DialogResult.Cancel depending on the button clicked by the user on the dialog box.

We access the public property "UserName" on the dialog box to access the value entered on the dialog box by the user.

Note that before displaying the dialog box, we set the "MyParentForm" property of the dialog box to allow the dialog box access to data from the parent form. In MDI mode, the child forms can access the Parent form using the ParentForm property.

private void button1_Click(object sender, System.EventArgs e)
{
Form2 oForm2 =
new Form2();
oForm2.MyParentForm =
this;
if (oForm2.ShowDialog() == DialogResult.OK)
label1.Text = "Hello " + oForm2.UserName;
else
label1.Text = "Hello Guest";
}

Forms In Action



Figure 3: Main Form



Figure 4: Dialog Box, The backcolor of the dialog box is based on the selection made by the user on the Parent Form



Figure 5: Main Form, The value entered by the user in the dialog box is accessed from the Dialog Box

Source Code :

Form1.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace TestApp
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
public System.Windows.Forms.ComboBox comboBox1;
public Form1()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1.Location = new System.Drawing.Point(16, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 32);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.label1.Location = new System.Drawing.Point(24, 56);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(248, 24);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
this.comboBox1.Items.AddRange(new object[] {"Red","Green","Blue","Yellow"});
this.comboBox1.Location = new System.Drawing.Point(24, 90);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(240, 21);
this.comboBox1.TabIndex = 2;
this.comboBox1.Text = "comboBox1";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 181);
this.Controls.AddRange(new System.Windows.Forms.Control[] this.comboBox1,this.label1,this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
[STAThread]
static void Main()
{
Application.Run(
new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Form2 oForm2 =
new Form2();
oForm2.MyParentForm =
this;
if (oForm2.ShowDialog() == DialogResult.OK)
label1.Text = "Hello " + oForm2.UserName;
else
label1.Text = "Hello Guest";
}
private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.SelectedIndex =0;
}
}
}

Form2.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace TestApp
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtname;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public Form1 MyParentForm;
public string UserName
{
get{return txtname.Text;}
}
public Form2()
{
this.txtname = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.txtname.Location = new System.Drawing.Point(16, 32);
this.txtname.Name = "txtname";
this.txtname.Size = new System.Drawing.Size(264, 20);
this.txtname.TabIndex = 0;
this.txtname.Text = "Please enter your Name here";
this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
this.button1.Location = new System.Drawing.Point(16, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 40);
this.button1.TabIndex = 1;
this.button1.Text = "OK";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button2.Location = new System.Drawing.Point(152, 80);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 40);
this.button2.TabIndex = 2;
this.button2.Text = "Cancel";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 149);
this.Controls.AddRange(new System.Windows.Forms.Control[]
this.button2,this.button1, this.txtname});
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
}
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
private void button1_Click(object sender, System.EventArgs e)
{
Close();
}
private void button2_Click(object sender, System.EventArgs e)
{
Close();
}
private void Form2_Load(object sender, System.EventArgs e)
{
Color clr = Color.FromName(((Form1)MyParentForm).comboBox1.SelectedItem.ToString));
this.BackColor = clr;
}
}
}

Conclusion

In this article we saw a simple way in which to access data fields and properties from the child forms and parent forms. You can extend this example to access any property, field, method or control value from the child forms/parent forms.

NOTE: This article is for purely educational purposes. This article is entirely original, unless specified. Any resemblance to other material is an un-intentional coincidence and should not be misconstrued as malicious, slanderous, or any anything else hereof.


Similar Articles