Using variables from other sources (C#)

Asked

Viewed 175 times

1

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

How do I write into 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’t access the string the same way.

  • Post your code so we can help you

1 answer

3


One possible way is to declare a constructor in the second window class 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();    
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.