Doubt - Project to the secondary screen and Form

Asked

Viewed 43 times

0

Follow the event code click:

//Estendido
telaSecundaria = new SegundaTela();
Screen[] telas = Screen.AllScreens;
Rectangle bounds = telas[1].Bounds; // pode ser outro índice.
telaSecundaria.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);
telaSecundaria.StartPosition = FormStartPosition.Manual;

telaSecundaria.Show();
telaSecundaria?.ChangeLabel(label1);

The above code works ok (showing normal on the secondary screen), as I do catch the result of the secondary screen and play on the Form1’s panel1 ?

Some solution ?

1 answer

1


There are many ways to do this. What I imagine is easier is to pass an object to your new Form than you need to use in your Form1

//form1
Form2 form2 = new Form2(seuResultado);
if(form2.ShowDialog() == DialogResult.OK)
{
    // adicione o que você quer fazer com o objeto alterado no seu form1
    // ex:
    textBox1.Text = seuResultado.alteracoesQuePreciso;
}

//form2
SeuResultado seuResultado;
public Form2(SeuResultado seuResultado)
{
    InitializeComponent();
    this.seuResultado = seuResultado;
}

In your form2 when you have finished modifying seuResultado inside your onClick button you confirm that the result is ready to be returned

seuResultado.alteracoesQuePreciso = TudoQuePrecisa;
DialogResult = DialogResult.OK;

And your form2 will close yourResulate will be ready to be used in Form1.

Thus their form1 stay 'on hold' until you finish your form2

If you need to leave both Forms open and work with them simultaneously, I think the easiest way is to pass a delegate to the form2.

In Form1 you add in your method of updating the Invokerequired check, to avoid Threads problem

public void Atualizar(SeuResultado seuResultado)
{
    if(InvokeRequired)
    {
        Invoke(new Action(() => Atualizar(seuResultado)));
        return;
    }
    textBox1.Text = seuResultado.alteracoesQuePreciso;
}

// para criar seu form2 você passa seu metodo de atualização como parametro
Form2 f = new Form2(Atualizar);
f.Show();

And the no form2 you modify the constructor with a delegate to update the Form1

Action<SeuResultado> action;
public Form2(Action<SeuResultado> action)
{
    InitializeComponent();
    this.action = action;
}

public void EnviarAtualizacao()
{
    //SeuResultado res
    action(res);
}
  • That one SeuResultado Voce created another form ?

  • No, it’s an object containing its result

Browser other questions tagged

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