Changing text from another form does not work

Asked

Viewed 193 times

1

Follows code:

Form2:

public void ChangeLabel(string s)
{
    labelX1.Text = s;
}

And in Form1:

private void button_MostrarSegundaTela_Click(object sender, EventArgs e)
{
    if (Screen.AllScreens.Length > 1)
    {
        //Estendido
        SegundaTela formulario = new SegundaTela();
        Screen[] telas = Screen.AllScreens;
        Rectangle bounds = telas[1].Bounds; // pode ser outro índice.
        formulario.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);
        formulario.StartPosition = FormStartPosition.Manual;
        formulario.Show();
    }
    else
    {
        //Duplicado, ou apenas 1 tela
        MessageBox.Show("Estender");
    }
}

private void label3_TextChanged(object sender, EventArgs e)
{
    var result = label3.Text;

    SegundaTela frm2 = new SegundaTela();
    frm2.ChangeLabel(result);
}

The idea is, when to change label from Form1, also change label form2.

It doesn’t work. Apparently nothing.

Some solution ?

  • Have you checked if the Modifiers property is the same as public? In private mode you won’t let it change outside the class.

  • Yes, I already checked the property and is as Modifiers:Public.

  • You are instantiating a form and changing the value of the label, ok so far. But where is the part that form is shown????

  • @LINQ edited post. Got better.

  • 1

    @Matheusmiranda realizes that it doesn’t make sense. You instantiate and display a form in the first if, in the quoted method a new instance of the form is created that is not even shown

  • Yes, I did it to make the screen black. When label for new, show...

  • 1

    Regardless of the reason, it makes no sense to create a new instance. Move the variable to a more comprehensive scope.

  • True, it makes sense.

  • I don’t have 2 monitors but it worked right when I did the changing test Screen.AllScreens.Length > 0 and Rectangle bounds = telas[0].Bounds

Show 4 more comments

1 answer

3


In the code you try to change the value of the label is being created a new instance form.

That is, the instance being shown is intact.

Move the form variable to a more comprehensive scope and use this variable to call the method that changes the label value.

Something like that:

public class SuaTela
{
    SegundaTela telaSecundaria;

    private void button_MostrarSegundaTela_Click(object sender, EventArgs e)
    {
        if (Screen.AllScreens.Length > 1)
        {
            telaSecundaria = new SegundaTela();
            // resto do código 
            telaSecundaria.Show();
        }
    }

    private void label3_TextChanged(object sender, EventArgs e)
    {
        var result = label3.Text;
        telaSecundaria?.ChangeLabel(result);
    }
}
  • The line: Form telaSecundaria; would not be SegundaTela telaSecundaria ? Because Changelabel doesn’t recognize.

  • 1

    No need pq Second Screen inherits from Form

  • 1

    @Matheusmiranda sorry there, my mistake. Yes you need

Browser other questions tagged

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