Call form only once

0

I have two forms, form1 and form2. The user must enter certain data in the form1, in textboxes, and the entered data must be confirmed, but I do it with another form (form2), which has 1 textbox and 2 buttons (confirm and cancel), and "appears" in front of the textbox in question when trying to exit the textbox. The process is as follows:

Go to textbox1 - > Try to exit texbox1, form2 appears asking to confirm the data. If the confirmation is correct, it goes to textbox2, otherwise it remains in form2. If you press Cancel, go back to texbox1.

The point is that there are 4 nested textboxes, so you must "jump" from texbox1 to texbox2, and to texbox3, and to textbox4.

And it works, but not correctly. The following are the methods in form1

void txtCopias_Leave(object sender, EventArgs e)
        {
            SacaTotal();
            Point locationOnForm = txtCopias.FindForm().PointToClient(txtCopias.Parent.PointToScreen(txtCopias.Location));
            using (ConfirmaContadores confConta = new ConfirmaContadores(int.Parse(txtCopias.Text)))
            {
                confConta.Location = new Point(locationOnForm.X, locationOnForm.Y + 25);
                var respuesta=confConta.ShowDialog();
                if (respuesta == DialogResult.Cancel)
                {
                    txtCopias.Focus();
                }
                else
                {
                    txtImpresion.Focus();
                }
                //confConta.Dispose();
            }
        }

        void txtImpresion_Leave(object sender, EventArgs e)
        {
            SacaTotal();
            Point locationOnForm = txtImpresion.FindForm().PointToClient(txtImpresion.Parent.PointToScreen(txtImpresion.Location));
            using (ConfirmaContadores confContadores = new ConfirmaContadores(int.Parse(txtImpresion.Text)))
            {
                confContadores.Location = new Point(locationOnForm.X, locationOnForm.Y + 25);
                confContadores.ShowDialog();
                var respuesta = confContadores.ShowDialog();
                if (respuesta == DialogResult.Cancel)
                {
                    txtImpresion.Focus();
                }
                else
                {
                    txtEscaner.Focus();
                }
                //confContadores.Dispose();
            }
        }

        void txtEscaner_Leave(object sender, EventArgs e)
        {
            SacaTotal();
            Point locationOnForm = txtEscaner.FindForm().PointToClient(txtEscaner.Parent.PointToScreen(txtEscaner.Location));
            using (ConfirmaContadores confirmaConta = new ConfirmaContadores(int.Parse(txtEscaner.Text)))
            {
                confirmaConta.Location = new Point(locationOnForm.X, locationOnForm.Y + 25);
                confirmaConta.ShowDialog();
                var respuesta = confirmaConta.ShowDialog();
                if (respuesta == DialogResult.Cancel)
                {
                    txtEscaner.Focus();
                }
                else
                {
                    txtFax.Focus();
                }
                //confirmaConta.Dispose();
            }
        }

And form2 has these methods:

private void btnConfirmar_Click(object sender, EventArgs e)
        {
            if (int.Parse(txtContadores.Text) == _contador)
            {
                DialogResult = DialogResult.OK;
                Dispose();
            }
            else
            {
                MessageBox.Show("Los datos ingresados no coinciden. Favor de verificar.", "Error en captura", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Dispose();
        }

As you can see, the Dispose(); I have in form2 (I read that when they are modal forms the Dispose () is manual). In this way, texbox1 works correctly, but when I try to leave texbox2 it marks Excepción no controlada del tipo 'System.ObjectDisposedException' . The same to go from texbox2 to 3 and from 3 to 4. Then I thought about Dispose(); from form1, I deleted the Dispose(); of form2 and I put them in form1 (where they are commented) but in this way, for some reason, from textbox2 I do not respect the click on the Confirm button (I must click it twice to close the form2).

Could someone explain to me why he does that, and how could he solve it?

Thanks for your time!

    
asked by antonio_veneroso 19.09.2017 в 16:17
source

1 answer

0

I already realized where the problem is: in the methods txtImpresion_Leave , txtEscaner_Leave , txtFax_Leave I have an extra line, confContadores.ShowDialog(); , which shows the confirmation window twice ...

Thanks to everyone who took the time to read the post.

    
answered by 20.09.2017 / 01:57
source