Hey this is too much easy. I am giving you a complete solution.
Here i have used two forms. The code of form1 is given value,
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 FormTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
The design file of the Form1 is ,
namespace FormTest
{
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.TxtBoxFrm1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// TxtBoxFrm1
//
this.TxtBoxFrm1.Location = new System.Drawing.Point(69, 53);
this.TxtBoxFrm1.Name = "TxtBoxFrm1";
this.TxtBoxFrm1.Size = new System.Drawing.Size(100, 20);
this.TxtBoxFrm1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.TxtBoxFrm1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
public System.Windows.Forms.TextBox TxtBoxFrm1;
}
}
Please look on that we need to make the control of Form1 is public if we want to access it from other Form. In our Case this is
public System.Windows.Forms.TextBox TxtBoxFrm1;
Now source code of Form2 is,
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 FormTest
{
public partial class Form2 : Form
{
private Form1 currentForm1=null;
public Form2(Form1 submittedForm1)
{
InitializeComponent();
currentForm1 = submittedForm1;
currentForm1.Show();
}
private void BtnFrm2_Click(object sender, EventArgs e)
{
string testStr = TxtBoxFrm2.Text;
currentForm1.TxtBoxFrm1.Text = testStr;
}
}
}
And Design Code is,
namespace FormTest
{
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.TxtBoxFrm2 = new System.Windows.Forms.TextBox();
this.BtnFrm2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// TxtBoxFrm2
//
this.TxtBoxFrm2.Location = new System.Drawing.Point(52, 30);
this.TxtBoxFrm2.Name = "TxtBoxFrm2";
this.TxtBoxFrm2.Size = new System.Drawing.Size(179, 20);
this.TxtBoxFrm2.TabIndex = 0;
//
// BtnFrm2
//
this.BtnFrm2.Location = new System.Drawing.Point(73, 137);
this.BtnFrm2.Name = "BtnFrm2";
this.BtnFrm2.Size = new System.Drawing.Size(131, 23);
this.BtnFrm2.TabIndex = 1;
this.BtnFrm2.Text = "Change Text";
this.BtnFrm2.UseVisualStyleBackColor = true;
this.BtnFrm2.Click += new System.EventHandler(this.BtnFrm2_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.BtnFrm2);
this.Controls.Add(this.TxtBoxFrm2);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox TxtBoxFrm2;
private System.Windows.Forms.Button BtnFrm2;
}
}
And main Program source code is,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace FormTest
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2(new Form1()));
}
}
}
That's all my source code. What you need to do is that
1. Create a WinForm Application
2. Add Form1. In its .cs file copy my code and Paste,in its Designer.cs file copy my design code for From1 and paste there.
3. Same thing do in Form2 and Main Program.
Run this. You will see there are two form running on the same time. Write something on the TextBox of Form2 and Press Button then you will see same text will appear in the textbox of From1