6
Answers

Pass data between forms

Phil Saville

Phil Saville

14y
9.2k
1
hi there, im having trouble trying to pass strings from my 2nd windows form to my 1st windows forms...

i have two simple forms, the first just has just a button on which opens the 2nd form so the 2 forms are open at the same time. On this 2nd form i then have a text box and another button, the user will enter some text into the textbox and then press the button. Once this button is pressed i would like the text to be passed to the first form. I have tried using the following code...

Form1:

public partial class Form1 : Form
    {
        string text;

        public Form1(string receivedText)
        {
            text = receivedText;
            InitializeComponent();
        }

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

        private void Form1_Load(object sender, EventArgs e)
        {
            if (text == "")
            {
            }
            else
            {
                label1.Text = text;
            }
        }       
    }


Form2:

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f = new Form1(textBox1.Text);
            this.Hide();          
        }       
    }


This does not seem to work though and i get the error..." 'Pass.Form1' does not contain a constructor that takes '0' arguments.

Please help!

Answers (6)