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

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.      ...
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# 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...
a) b) c) Compare and contrast systems programming in Windows as against systems programming in Unix/Linux....
a) b) c) Compare and contrast systems programming in Windows as against systems programming in Unix/Linux. CR, 8 Explain the term structured exception handling (SEH) as used in systems programming and give a practical example of how it can be used to handle errors in a block of code. AP, 7 Write a C/C++ system program to delete an unwanted file in the Windows file system. Compile and run the program and copy the source code into your answer booklet....
C Programming build a function that is outside of main domain but can be called Build...
C Programming build a function that is outside of main domain but can be called Build a function that will : - Take 3 arrays and their lengths as input: Array 1 for even numbers, Array 2 for odd numbers. Both 1 and 2 have the same size - put all elements in array 3 elements from array 1 should be placed in the even indexes and elements of array 2 should be placed in the odd indexes, in increasing...
***IN C# ONLY, USING WINDOWS FORMS*** --NO JAVA--. Create a GUI application in C# that calculates...
***IN C# ONLY, USING WINDOWS FORMS*** --NO JAVA--. Create a GUI application in C# that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide: • Number of days on the trip • Amount of airfare, if any • Amount of car rental fees, if any • Number of miles driven, if a private vehicle was used • Amount of parking fees, if any • Amount of...
Must be in Visual C# using windows forms : Create an application that opens a specified...
Must be in Visual C# using windows forms : Create an application that opens a specified text file and then displays a list of all the unique words found in the file. Hint: Use a LINQ method to remove all duplicate words.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT