Using variables from other forms (C #)

1

I'm learning programming with C #, using visual studio 2013. In this program I have 2 windows. In the first window I have a text box and a button that writes the text to a string and opens the second window. In the second window I only have one text box.

How do I write in the text box of window 2 what is inside the string of window 1? Note: I tried to make the string public but I can not access the string in the same way.

    
asked by anonymous 06.10.2015 / 12:36

1 answer

2

One possible way is to declare a constructor in the class of the second window that receives a string :

public partial class Form2 : Form
{
    private string texto;

    public Form2(string texto)
    {
        InitializeComponent();
        this.texto = texto;

        //utilize a variável texto para "setar" a sua caixa de texto
        textBox1.Text = texto;
    }
    .....
    .....
}

To open the second window use this constructor:

private void button1_Click(object sender, EventArgs e)
{

    Form2 form2 = new Form2(textBox1.Text);
    form2.Show();    
}
    
06.10.2015 / 13:32