In: Computer Science
C# Programming
Discuss how to build a Multi-Form Windows application in C#. That is an App that has more than one form, like say maybe 5 forms. How do you connect these forms and pass data objects between these forms?
In case of any query do comment. Please rate answer as well. Thanks
Code:
===From1.cs==
using System;
using System.Windows.Forms;
namespace MultiFormAppDisplay
{
public partial class Form1 : Form
{
public string Data { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this.textBox1.Text);
frm.ShowDialog();
this.textBox1.Text = frm.Data;
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// showing and passing data back to the property can be further used
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Deactivate(object sender, EventArgs e)
{
this.Data = this.textBox1.Text;
}
}
}
====Form1.designer.cs===
namespace MultiFormAppDisplay
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(224, 256);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(165, 62);
this.button1.TabIndex = 0;
this.button1.Text = "Pass Data to Other Form";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(220, 108);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(87, 20);
this.label1.TabIndex = 1;
this.label1.Text = "Enter Data";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(224, 179);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 26);
this.textBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Deactivate += new System.EventHandler(this.Form1_Deactivate);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
}
}
===From2.cs===
using System;
using System.Windows.Forms;
namespace MultiFormAppDisplay
{
public partial class Form2 : Form
{
public string Data { get; set; }
public Form2()
{
InitializeComponent();
}
/// <summary>
/// This constructor is used to pass data between forms
/// </summary>
/// <param name="data"></param>
public Form2(string data)
{
this.Data = data;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.textBox1.Text = this.Data;
}
private void button1_Click(object sender, EventArgs e)
{
Form3 frm = new Form3(this.textBox1.Text);
frm.ShowDialog();
this.textBox1.Text = frm.Data;
}
/// <summary>
/// showing and passing data back to the property can be further used
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form2_Deactivate(object sender, EventArgs e)
{
this.Data = this.textBox1.Text;
}
}
}
===From2.designer.cs===
namespace MultiFormAppDisplay
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(144, 274);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(165, 62);
this.button1.TabIndex = 1;
this.button1.Text = "Pass Data to Other Form";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(140, 99);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(185, 20);
this.label1.TabIndex = 2;
this.label1.Text = "Data from Previous Form";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(144, 160);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 26);
this.textBox1.TabIndex = 3;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.Deactivate += new System.EventHandler(this.Form2_Deactivate);
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
}
}
===From3.cs===
using System;
using System.Windows.Forms;
namespace MultiFormAppDisplay
{
public partial class Form3 : Form
{
public string Data { get; set; }
public Form3()
{
InitializeComponent();
}
/// <summary>
/// This constructor is used to pass data between forms
/// </summary>
/// <param name="data"></param>
public Form3(string data)
{
this.Data = data;
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
this.textBox1.Text = this.Data;
}
private void button1_Click(object sender, EventArgs e)
{
Form4 frm = new Form4(this.textBox1.Text);
frm.ShowDialog();
this.textBox1.Text = frm.Data;
}
/// <summary>
/// showing and passing data back to the property can be further used
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form3_Deactivate(object sender, EventArgs e)
{
this.Data = this.textBox1.Text;
}
}
}
===From3.designer.cs===
namespace MultiFormAppDisplay
{
partial class Form3
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(203, 259);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(165, 62);
this.button1.TabIndex = 1;
this.button1.Text = "Pass Data to Other Form";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(199, 94);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(185, 20);
this.label1.TabIndex = 3;
this.label1.Text = "Data from Previous Form";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(203, 143);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 26);
this.textBox1.TabIndex = 4;
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form3";
this.Text = "Form3";
this.Deactivate += new System.EventHandler(this.Form3_Deactivate);
this.Load += new System.EventHandler(this.Form3_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
}
}
===From4.cs===
using System;
using System.Windows.Forms;
namespace MultiFormAppDisplay
{
public partial class Form4 : Form
{
public string Data { get; set; }
public Form4()
{
InitializeComponent();
}
/// <summary>
/// This constructor is used to pass data between forms
/// </summary>
/// <param name="data"></param>
public Form4(string data)
{
this.Data = data;
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
this.textBox1.Text = this.Data;
}
private void button1_Click(object sender, EventArgs e)
{
Form5 frm = new Form5(this.textBox1.Text);
frm.ShowDialog();
this.textBox1.Text = frm.Data;
}
/// <summary>
/// showing and passing data back to the property can be further used
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form4_Deactivate(object sender, EventArgs e)
{
this.Data = this.textBox1.Text;
}
}
}
===From4.designer.cs===
namespace MultiFormAppDisplay
{
partial class Form4
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(148, 278);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(165, 62);
this.button1.TabIndex = 1;
this.button1.Text = "Pass Data to Other Form";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(144, 101);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(185, 20);
this.label1.TabIndex = 3;
this.label1.Text = "Data from Previous Form";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(148, 191);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 26);
this.textBox1.TabIndex = 4;
//
// Form4
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form4";
this.Text = "Form4";
this.Deactivate += new System.EventHandler(this.Form4_Deactivate);
this.Load += new System.EventHandler(this.Form4_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
}
}
===From5.cs===
using System;
using System.Windows.Forms;
namespace MultiFormAppDisplay
{
public partial class Form5 : Form
{
public string Data { get; set; }
public Form5()
{
InitializeComponent();
}
/// <summary>
/// This constructor is used to pass data between forms
/// </summary>
/// <param name="data"></param>
public Form5(string data)
{
this.Data = data;
InitializeComponent();
}
private void Form5_Load(object sender, EventArgs e)
{
this.textBox1.Text = this.Data;
}
private void button1_Click(object sender, EventArgs e)
{
}
/// <summary>
/// showing and passing data back to the property can be further used
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form5_Deactivate(object sender, EventArgs e)
{
this.Data = this.textBox1.Text ;
}
}
}
===From5.designer.cs===
namespace MultiFormAppDisplay
{
partial class Form5
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(162, 299);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(165, 62);
this.button1.TabIndex = 1;
this.button1.Text = "Pass Data to Other Form";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(158, 123);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(185, 20);
this.label1.TabIndex = 3;
this.label1.Text = "Data from Previous Form";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(162, 209);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 26);
this.textBox1.TabIndex = 4;
//
// Form5
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form5";
this.Text = "Form5";
this.Deactivate += new System.EventHandler(this.Form5_Deactivate);
this.Load += new System.EventHandler(this.Form5_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
}
}
=========screen shot of the code===
Output:
Passing back to calling form as well:
Lets say you updated data in form5, now close the form:
Same way you can update in other forms as well.