I can not open a form more than once from another form

0

In my project I use mdiParent, I have an open form with showdialog () and from this I invoke another form, the problem is that it only lets me do it once, that is to say I open it for the first time, I close it then I try to open it again and it does not. Here the code:

  private void btnVerDespachos_Click(object sender, EventArgs e)
    {
        try
        {
            FrmConsultarDespacho frm = Application.OpenForms["FrmConsultarDespacho"] as FrmConsultarDespacho;

            if (frm != null)
            {
                frm.CodigoFactura = _NumFact;
                frm.WindowState = FormWindowState.Normal;
                frm.BringToFront();
            }
            else
            {
                FrmConsultarDespacho llamar = new FrmConsultarDespacho();
                llamar.CodigoFactura = _NumFact;
                llamar.MdiParent = FrmPrincipal.pantalla;
                llamar.Show();
                llamar.BringToFront();
            }
            this.Close();
        }
        catch (Exception ex)
        {
            Funciones.MensagedeError(ex.Message);
        }            
    }
    
asked by avargasma 23.02.2017 в 21:58
source

1 answer

1

You can implement e.Cancel in the event of the closing of the "FrmConsultDisplay" form, in this way you can prevent the instance from being deleted and you can continue to use it:

private void FrmConsultarDespacho_FormClosing(object sender, FormClosingEventArgs e)
{
    //Evita el cierre del formulario
    e.Cancel = true;
    this.Hide();
}

in the event your button should show the form:

  try
  {....
        FrmConsultarDespacho frm....

        if (frm != null)
        {
            frm.CodigoFactura = _NumFact;
            frm.WindowState = FormWindowState.Normal;
            frm.BringToFront();
            frm.Show(); /***Aqui se muestra el formulario***/

        }
        else
        {....
    
answered by 23.02.2017 в 22:31