Friday 6 December 2013

Simple Example To Send Data From One Form To Another Form In C#

Design Form1 And Form2 As Below Shown:



Now, Write Below The Code In Form1.cs :

using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;

namespaceSendStringFromOneFormToAnotherForm
{
    public partial class Form1 : Form
    {
        publicForm1()
        {
            InitializeComponent();
        }

        privatevoid button1_Click(objectsender, EventArgs e)
        {
            Form2frm2 = new Form2(textBox1.Text);
            frm2.Show();
        }
    }

}

Now, Write The Below Code In Form2.cs :

using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;

namespaceSendStringFromOneFormToAnotherForm
{
    public partial class Form2 : Form
    {
        publicForm2(string str)
        {
            InitializeComponent();
            textBox1.Text = str;
        }
    }
}

Now, Run The Application And Give Any Text In The TextBox And Click On "Show Data In Other Form" Button, Then It Shows The Output As Below :

Output:-


No comments:

Post a Comment

Note: only a member of this blog may post a comment.