MensageBox is in loop

1

When I start my project WinForms I set focus on a lookUp Edit component of DevExpress that has a Leave event. The purpose of this focus is to make target selection compulsory. The initial value of this component is null and is populated with bank data. When clicking on another element, for example: a menu that is in the main form, an alert is displayed but it is looped. I need that after clicking on another element, the alert is displayed and the focus goes back to lookup Edit destino .

In my opinion, the loop happens because the default value of the destination is = null and after return it ends up falling in the same condition. But I could not find a way to fix this.

The default value of the Encerrar é false variable and the default value of the destino é null component

How can I resolve this? Below is a snippet of code.

 private void Destino_Leave(object sender, EventArgs e)
{
    if (!Encerrar)
    {
        if (destino.EditValue == null)
        {
            MessageBox.Show("Selecione um Destino");
            ActiveControl = destino;
            destino.Focus();
            return;
        }
         LugarDestino = destino.EditValue.ToString();


         ...restante do código...
    }
}
    
asked by anonymous 20.04.2018 / 20:54

3 answers

0

After some tests it was noted that the Leave event was not suitable for this situation. The alert was displayed every time this event happened and also because the default value is equal to null , that is, when initiating focus was assigned in the Destination component, however, because its default value Null the alert was displayed by doing with the Leave event fired again causing the terrible loop ...

The solution was to change the event from Leave to Validating because in this way it was possible to validate the value of the target component even if the value is null , after which the alert was once displayed. In terms of system usability it was better to change the function Focus() by ShowPopup() .

Below is the updated code snippet:

private void Destino_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (!Encerrar)
    {
        if (destino.EditValue == null)
        {
            MessageBox.Show("Selecione um Destino");
            ActiveControl = destino;
            destino.ShowPopup();
            return;
        }
         LugarDestino = destino.EditValue.ToString();


         ...restante do código...
    }
}
    
24.04.2018 / 22:02
0

When I was developing using devexpress, I had some problems like this, so I did not use focus anymore, but try the code below.

I believe that when I use activecontrol and focus, the focus of the object is being removed and placed, making an eternal loop.

private void Destino_Leave(object sender, EventArgs e)
{
    if (!Encerrar)
    {
        if (destino.EditValue == null)
        {
            MessageBox.Show("Selecione um Destino");
            return;
        }
         LugarDestino = destino.EditValue.ToString();


         ...restante do código...
    }
}
    
22.04.2018 / 11:16
0

Once you enter the event you disable it, before you leave active again.

    private void Destino_Leave(object sender, EventArgs e)
    {
        //desativando o evento
        destino.Leave -= Destino_Leave;
        if (!Encerrar)
        {
            if (destino.EditValue == null)
            {
                MessageBox.Show("Selecione um Destino");
                ActiveControl = destino;
                destino.Focus();
                return;
            }
            LugarDestino = destino.EditValue.ToString();
        }
        //ativando o evento
        destino.Leave += Destino_Leave;
    }
    
23.04.2018 / 22:17