Would it be possible to pass a string from one form to another? [closed]

-4

Imagine a Form that asks you for a color, then it would have a TextBox , and from that TextBox I wanted it to Press the "OK" button for example, this sends the text from this TextBox to another Form , so that it could appear in a Label of that Form with the text when you opened it, how could you do it?

    
asked by anonymous 09.12.2015 / 15:22

1 answer

4

Obviously you have two forms, let's call the main form of Form1 and the secondary of Form2

In% main% you will have a button with a click event similar to below

private void button_Click(object sender, EventArgs e){
    //Aqui você vai pegar o valor do textBox
    string valor = textBoxCor.Text;

    //Aqui chamar o outro form passando o valor como parâmetro
    var form = new Form2(valor);
    form.ShowDialog();
}

To do this, you need to add another constructor (or change the existing constructor) in the second form.

public Form2(string cor){
    InitializeComponent();
    labelCor.Text = cor; //O label está recebendo o valor que foi recebido por parâmetro
}

Related: What is a builder for?

    
09.12.2015 / 16:21