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

Asked

Viewed 1,531 times

-4

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

  • 1

    Welcome to Stack Overflow Tour and edit your question.

  • First, you don’t need to tag visual-studio because your problem doesn’t have none relation to your IDE. Second, you don’t need to put the language in the question title, put it as a tag - that’s what they’re for. Third, have you tried anything? Post the code you already have so we can help you better, if you have not tried anything explain your problem better and put some pseudo-code if you think it can help our understanding. Finally, does the answer below meet your needs? If not, I will give you a more detailed answer.

  • You can always improve your questions/answers by clicking [Edit].

  • By the way, he’s already done the tour @Marconciliosouza

  • @jbueno I’ve already edited the title. I don’t know what else to add to help you, I simply ask for an example of a way to pass a string one way to the other as the title question, and the answer below does not meet my needs, no.

  • Well, there is missing information: the second form will already be open or will be opened when you press the button?

  • I didn’t really pay attention to that detail because I knew how to open a new shape by pressing the button, but I already edited the question, I hope it’s already better

  • @jbueno, when he had posted he had not yet done.

  • It had been almost an hour that he had done :P But without stress, it was just to comment really

  • xD deiam - me a discount, I’m only 14 years old come on..

Show 5 more comments

1 answer

4


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

In the Form1 main you will have a button with click event similar to the 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();
}

For this to work, you will 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 good is a builder?

Browser other questions tagged

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