2
Reply

how to transfer text from a textbox of one form to another forms textbox?

javed khan

javed khan

15y
7.7k
0
Reply

    Consider Form1 contains a textbox named textBox1 and a button named button1
    Form2 contains no controls .

     When user clicks on button1 textBox1's text    will be transfered from form1 to form2 by writing this code.

    Code for Form1 :

    private void button1_Click(object sender, EventArgs e)

    {

    this.Hide();

    Form2 f2 = new Form2();

    TextBox text=new TextBox ();

    f2.Controls.Add(text);

    text.Text = textBox1.Text;

    f2.Show();

    }


     

    Consider Form1 contains a textbox named textbox1 and a button named button1
    Form2 contains a textbox named textbox1. When user clicks on button1 text will be transfered from form1 to form2 by writing this code.

    We are passing the reference of form1 to form2.

    Code for Form1 :

    private void button1_Click()
    {
    Form2 obj=new Form2(this);
    obj.show();
    }


    Code for Form2:


    Form1 obj;
    public Form1(Form1 obj1)
    {
     obj=obj1;
     InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
    textbox1.Text=obj.textbox1.Text;
    }