Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to pass or transfer data between windows forms in c# (.net 2.0).

Author: Zunnair
Download Source Code : 830_data tranfer.zip

In this simple article you will learn how to transfer or pass data between windows forms in C# .net 2.0.

 There are different methods which can be used for this purpose.

1)       By Making Public Variables:

You can transfer the data between forms by making public variables.

Public variables of a form can be accessed in another form.

To demonstrate, make a window application. Drag one textbox on form1. Now go in solution explorer, do right click on application name than click on add than on new items and add a new window form.

Now drag one textbox and button on form2. Make a public string variable to transfer the string written in form2 textbox.

Now write the following code on FORM1 LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

  {

     this.Text = "Devasp.net Data Transfer Application";

     Form2 obj = new Form2();

     obj.ShowDialog();

     txtform1.Text=obj.str;     

  }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Dim obj As New Form2()

        obj.ShowDialog()

        Me.Text = "Devasp.net Data Transfer Application"

        txtform1.Text = obj.str

    End Sub

Thus code will show the form2 where you will write the string in textbox.

After writing the string in textbox press button to transfer data.

Now write the following code on button click event:

C#

 

 

 

private void btnTrtanfer_Click(object sender, EventArgs e)

        {

            str = txtform2.Text;

            this.Close();

 

        }

VB

Private Sub btnTrtanfer_Click(ByVal sender As Object, ByVal e As EventArgs)

        Str = txtform2.Text

        Me.Close()

    End Sub

This code will store data in public string variable which can be accessed in form1.

Now write code on FORM2 LOAD Event.

C#

private void Form2_Load(object sender, EventArgs e)

        {

            this.Text = "Devasp.net Data Transfer Application";

 

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "Devasp.net Data Transfer Application"

        End Sub

2)       By Making Controls Public:

You can also transfer the data of one from to another by making the controls (textboxes) public. To make controls public go in designer and change the access.

See the following designer code.

namespace data_transfer2

{

    partial class Form2

    {

        ///

        /// Required designer variable.

        ///

        private System.ComponentModel.IContainer components = null;

 

        ///

        /// Clean up any resources being used.

        ///

        /// true if managed resources should be disposed; otherwise, false.

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

 

        #region Windows Form Designer generated code

 

        ///

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        ///

        private void InitializeComponent()

        {

            this.txtform2 = new System.Windows.Forms.TextBox();

            this.btnclose = new System.Windows.Forms.Button();

            this.SuspendLayout();

            //

            // txtform2

            //

            this.txtform2.Location = new System.Drawing.Point(78, 44);

            this.txtform2.Name = "txtform2";

            this.txtform2.Size = new System.Drawing.Size(100, 20);

            this.txtform2.TabIndex = 0;

            //

            // btnclose

            //

            this.btnclose.Location = new System.Drawing.Point(89, 115);

            this.btnclose.Name = "btnclose";

            this.btnclose.Size = new System.Drawing.Size(75, 23);

            this.btnclose.TabIndex = 1;

            this.btnclose.Text = "close";

            this.btnclose.UseVisualStyleBackColor = true;

            this.btnclose.Click += new System.EventHandler(this.btnclose_Click);

            //

            // Form2

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(292, 273);

            this.Controls.Add(this.btnclose);

            this.Controls.Add(this.txtform2);

            this.Name = "Form2";

            this.Text = "Form2";

            this.ResumeLayout(false);

            this.PerformLayout();

 

        }

 

        #endregion

 

        public System.Windows.Forms.TextBox txtform2;

        private System.Windows.Forms.Button btnclose;

    }

In this code you can see that textbox is public noe it can be accessed in form1.

To demonstrate, make a window application. Drag one textbox on form1. Now go in solution explorer, do right click on application name than click on add than on new items and add a new window form.

Now drag one textbox and button on form2. Make a public string variable to transfer the string written in form2 textbox.

Now write the following code on FORM1 LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text="Devasp.net Data Transfer Application";

            Form2 obj=new Form2();

            obj.ShowDialog();

            textBox1.Text = obj.txtform2.Text;

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "Devasp.net Data Transfer Application"

        Dim obj As New Form2()

        obj.ShowDialog()

        textBox1.Text = obj.txtform2.Text

    End Sub

This code will show the form2 write string in textbox and close the form. That string can be directly accessed in form1 textbox.

3)       By Making Common Class:

By making a common class between the forms you can transfer data. Make the class variables static and public so they can be accessed in all forms. Store forms value in that variables and access them in other forms.

This simple article tells you how you can transfer data between windows forms using c# .net 2.0.

Article Comments
how can i load form2 when form 2 have label1

and form1 have textbox ??

Posted on 12/16/2009 10:15:29 PM by juniel

nice post. It really helps me. i was in stuck how to pass text between 2 forms.
Thanks.........

Posted on 5/20/2010 12:30:31 AM by Razib

textboxes have a property named "modifier". change it to public then you can control it from any class or form. but do it before frm.showdialog();

Posted on 7/24/2010 10:31:00 PM by Ryan

how send the complete one row on next page on select checkbox in the grid view of one page

Posted on 11/10/2010 3:31:44 AM by Avdhesh

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< String operations in C# .net 2.0. (Part 3)

Disclaimer - Privacy
© 2002-2017 DevASP.net