Question

In: Computer Science

C# Programming Discuss how to build a Multi-Form Windows application in C#. That is an App...

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?

Solutions

Expert Solution

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.


Related Solutions

C# Programming; Create a Windows Form Application that displays a scrollable list of 10 random integers...
C# Programming; Create a Windows Form Application that displays a scrollable list of 10 random integers in the range of 1 to 100. The form should also have (1) an Add button (and input textbox) for the user to add a new number to the list, (2) a Delete button to delete the current selected integer, (3) a Sort button to sort the list, (4) a Reverse button to display the list in reverse order, and (5) Display Multiple (and...
Write a Windows Form application named SumFiveInts. Microsoft Visual C#: An Introduction to Object-Oriented Programming,7th Edition....
Write a Windows Form application named SumFiveInts. Microsoft Visual C#: An Introduction to Object-Oriented Programming,7th Edition. Ch. 5, page 220. Take snip of program results.
How to make an application for windows using c# ?
How to make an application for windows using c# ?
A, B:   Design and Implement a C# windows form application to ask the user for 10...
A, B:   Design and Implement a C# windows form application to ask the user for 10 integer numbers, sort them in ascending order and display the sorted list. Use bubble sort technique to sort the array elements and do not use any built-in sort method to sort the array elements.                                                        [02] C:    Test and evaluate your program by inputting variety of values.
A, B:    Design and Implement a C# windows form application to encrypt and decrypt text....
A, B:    Design and Implement a C# windows form application to encrypt and decrypt text. The application use to receive a string and display another encrypted string. The application also decrypt the encrypted string. The approach for encryption/decryption is simple one i.e. to encrypt we will add 1 to each character, so that "hello" would become "ifmmp", and to decrypt we would subtract 1 from each character.    C:   Test and evaluate application by applying different strings.      ...
Create a Windows application in C# that can be used to change the form color. Your...
Create a Windows application in C# that can be used to change the form color. Your form background color should initially be blue. Provide at least two buttons with two different color choices and a Reset option. Change the font style and size on buttons. Align the buttons so that they are in the center of the form. The color choice buttons should be the same size. Add event handlers for the buttons so that when the user click the...
Create a windows form application in C# that looks and functions like a basic calculator. It...
Create a windows form application in C# that looks and functions like a basic calculator. It must do the following: Add, subtract, multiply, and divide. Account for division by zero. Show at least two decimal places. Retain the last number on the window so that, when the user opens the calculator the next time, the number that was in the window at the last close appears. Have a memory, so that users can store values and recall them. Operate with...
Using C# Windows App Form Create a simple calculator using +,-,*,/ Please show code GUI Code...
Using C# Windows App Form Create a simple calculator using +,-,*,/ Please show code GUI Code for calculator menus radio button input text boxes
C# Programming using Windows Form 1. Define a variable named isTrue that stores a value of...
C# Programming using Windows Form 1. Define a variable named isTrue that stores a value of whether something is true or false. Set this variable    to the negative. Output this variable to the txtIsTrue textbox. 2. Define a variable named favoriteGame that could store the name of a game. Set this variable's value to your favorite game. Output this variable to the txtFavoriteGame textbox. 3.. Define a variable named pi that can store real numbers. initialize it to a value...
C# windows application form. Create a base class to store characteristics about a loan. Include customer...
C# windows application form. Create a base class to store characteristics about a loan. Include customer details in the Loan base class such as name, loan number, and amount of loan. Define subclasses of auto loan and home loan. Include unique characteristics in the derived classes. For example you might include details about the specific auto in the auto loan class and details about the home in the home loan class. Create a presentation class to test your design by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT