locked
How to show form2 textbox data in form1 textbox? RRS feed

  • Question

  • I have 2 forms form1 and form2. First when you run the application form1 will open. In form1 there is one text box and one button. Do not enter any text in that textbox. Please just click on the button. After clicking the button form2 should be displayed. But, do not close form1. In form2 also there is one text box and one button. Now enter some text in the form2 textbox and click the button. The text should be displayed in the form1 textbox.
    Wednesday, March 24, 2010 7:04 AM

Answers

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

            private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2(this);
                frm.Show();
            }
        }

    public partial class Form2 : Form
        {
            Form _frm;
            public Form2(Form frm)
            {
                _frm=frm;
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
               Form1 objMain = (Form1)_frm ;
                objMain.textBox1.Text = "Hello";
            }
        }
    Wednesday, March 24, 2010 7:28 AM

All replies

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

            private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2(this);
                frm.Show();
            }
        }

    public partial class Form2 : Form
        {
            Form _frm;
            public Form2(Form frm)
            {
                _frm=frm;
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
               Form1 objMain = (Form1)_frm ;
                objMain.textBox1.Text = "Hello";
            }
        }
    Wednesday, March 24, 2010 7:28 AM
  • firstly  u should change the modifiers property of the button on form1 as public.

    now u can reach that from form2.

     

    Button1 on form1 click event

    Create a new Form2 writing

         "Form2 frm2=new Form2();"

         "frm2.Show();"

    form2 will be appear

     

    Button1 on form2 click event

    Create a new Form1 writing

         "Form1 frm1=new Form1();"

         "frm.textBox1.Text=textBox1.Text;  //the right textBox1 is on form2

         "this.Close();"  // close form2

         "frm1.Show();"


    please, mark this as answer if it is THE answer
    • Edited by TamerCE Thursday, March 25, 2010 11:51 AM
    Wednesday, March 24, 2010 9:20 AM
  • Hi TamerCE,

    In form2 class, you create a new instance of Form1, which is not the object, wherein show form2. So OP's reply is right.

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Thursday, March 25, 2010 8:55 AM
  • Hi TamerCE,

    In form2 class, you create a new instance of Form1, which is not the object, wherein show form2. So OP's reply is right.

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    hi Guang-Ming Bian - MSFT

    i have tried this before posting. so that must be another solution which solves his problem. my solution is easier to understand so more affective i think.

    thnx for your interest anyway.


    please, mark this as answer if it is THE answer
    Thursday, March 25, 2010 11:56 AM