Transfer data from one form to another form in Windows Application using C#
How to pass the data from one form to another form in Windows Application using C#?. When we come across the situation to use the first page controls data in second page in windows application is bit difficult. Today I am going to explore all the possible ways to send the data from one page to another page in C# windows application. Transfer data from one form to another form in Windows Application using C#
How to transfer the data from one form to another form in Windows Application using C#
We can transfer the variable data, controls from one windows page to another windows page form by using C#. Using Constructer:
This is very simple method to pass the values and control information from one Form1 to SecondForm. Here I have created two windows forms Form1 and SecondForm. I have declare the textbox controls and string in the constructor of a SecondForm. Now I am passing the controls and string from the first form button click. First Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FirstForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string surName="Bokku";
SecondForm child = new SecondForm(textBox1,textBox2,surName);
child.Show();
this.Hide();
}
}
} Second Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FirstForm
{
public partial class SecondForm : Form
{
string first;
string second;
string surName;
public SecondForm(TextBox myBox1,TextBox myBox2, string surName2)
{
first = myBox1.Text;
second = myBox2.Text;
surName = surName2;
InitializeComponent();
}
private void SecondForm_Load(object sender, EventArgs e)
{
textBox1.Text=first;
textBox2.Text = second;
textBox3.Text = surName;
}
}
} Using Properties:
This is also one of the easiest mechanism to pass the data from one page to another windows page in C#. Here in the Form1 I have created one property name as textBoxForm1 and assigned the values of textBox1.
Now I have created one more property named textBoxForm2 in Form2 and assigned the value to label1 to display the value which is passing from Form1.Now created the Instance of Form2 and assigned the value to a property textBoxForm2 which we have in form2. First Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.textBoxForm2 = textBoxForm1;
frm.Show();
}
public string textBoxForm1
{
get { return textBox1.Text; }
}
}
} Second Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string textBoxForm2
{
set { label1.Text = value; }
}
}
} Using Delegate:
This is bit complex mechanism to pass the values from one form to another using c#. Here I have created one delegate named PassDataToSecondForm with input put parameter as textbox control. Created one property in Form2 and called it from the Form1 button event like as shown in code snippet. First Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void PassDataToSecondForm(TextBox text);
private void button1_Click(object sender, EventArgs e)
{
Form2 Myfrm2 = new Form2();
PassDataToSecondForm mySimpledel = new PassDataToSecondForm(Myfrm2.myData);
mySimpledel(this.textBox1);
Myfrm2.Show();
}
}
} Second Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void myData(TextBox txtForm1)
{
label1.Text = txtForm1.Text;
}
}
}
Apart from these we have several mechanism also, but these are fair enough to pass the data or controls from one form to another form.
wonderful 1, When I loved scanning ones scenario. I really appreciate your superb know how plus the time you place within educating average folks.