When you click the form 2 button update form 1 label

Asked

Viewed 836 times

0

How can I pull the label from Form1 to form2 to label. Refresh?

  • What kind of application?

  • Winforms, I forgot to put

  • https://answall.com/q/102839/18246

1 answer

0


When you instantiate form 2 when building it (Open the form), you need to pass a reference to the form 1 object (Previous Form), that is to have a form 1 property. So that in form 2 you call a public method that updates, or directly access the property of the object if it is public. Studying object orientation helps you understand better.

Example of what the class of the two forms would look like: Form 1

public partial class Form1 : Form
{
    public label labelNome;

    public void AbrirForm2()
    {
        Form2 segundoForm = new Form2();
        segundoForm.form1Reference = this;
    }
}

Form 2

public partial class Form2 : Form
{
    public Form1 form1Reference = null;

    public void EventoQueAlteraLabelDoForm1()
    {
        if(form1Reference != null)
            form1Reference.labelNome.text = "Hello World";
    }
}
  • I don’t quite understand, I’m a beginner in c#

  • I tried to make it easier to understand. You got it ?

  • I wanted to edit an existing label in Form 1

  • I got this error: Undefined object reference for an instance of an object.'

Browser other questions tagged

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