How to control the Leave or Validated event of a textbox inside an MDI container? C #

0

The detail is that I have a form which I call frmPrincipal which has the property MDiContainer as true, when starting the command form call as child a form called frmArticulo , the main idea is that if at the time of registering a new article and the key of the unit of measurement does not exist in the database, can open the new unit of measure capture form automatically.

   3 forms(frmPrincipal,frmArticulo,frmUnidadMedida)

        //Evento load del formulario principal
        private void frmPrincipal_Load(object sender, EventArgs e)
            {
                Articulos frm1 = new Articulos();
                frm1.MdiParent = this;
                frm1.Show();
            }

        //Envento Leave del textbox unidad de medida en form articulo
        private void txtUnidadMedida_Leave(object sender, EventArgs e)
            {
                //El metodo BuscarUnidadMedida consulta la DB y devuelve true
                //si se encuetra registrado, si no abre el form unidad medida
                //para registrar dicho valor
                if (!Consulta.BuscarUnidadMedida(txtUnidadMedida.Text))
            {
                frmUnidadMedida frm2 = new frmUnidadMedida();
                frm2.MdiParent = this.ParentForm;
                frm2.Show();
            }

        }

With what I find is that when the txtUnitedMeasure loses focus it searches the database and if it does not find the value, it opens the form to capture the new unit of measure, but this event is executed twice, so the result on the screen is that the frmUnityMeasure is displayed twice.

I can not find it because when you first open the form frmUnityMeasure the event of txtUnityMeasure_Leave is again executed.

If someone has found himself in a similar case, he could guide me about it.

    
asked by Eliziel Sauceda 13.06.2017 в 23:47
source

1 answer

0

You should try to use the event LostFocus instead of Leave .

Your problem can happen since the Leave event is executed by several actions and therefore the form is opened twice, being desired once only.

    
answered by 14.06.2017 в 15:36