Mensagebox is in loop

Asked

Viewed 78 times

1

When starting my project WinFormsi define focus on a component lookUp Edit of DevExpress who owned an event Leave. The goal of this focus is to make target selection mandatory. The initial value of this component is null and is filled in with bank data. When you click on another element, for example: a menu that is in the main form, an alert is displayed but this is in loop. I need that after clicking on another element, the alert is displayed and the focus is back on lookup Edit destino.


In my view, the loop happens because the default target value is = null and after the return it ends up falling into the same condition. But I couldn’t find a way to fix it


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


How can I resolve this? Below is a fragment of the 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...
    }
}
  • Destino_Leave appears to be an event handler. Of what object this handler signed and what event (who is sender)? That action Leave is the equivalent of LostFocus of the default controls?

3 answers

0

When I was developing using devexpress, I had some problems like this, so I no longer used "Focus", but try with the code below.

I believe that when using 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...
    }
}
  • If removing the focus works, however I need the focus to be mandatory on the component, that is, when I click on another component the alert is displayed and the focus is set on the component... the problem is that this way, to focus the component, is in loop :(

0

Once you enter the event disables it, before leaving 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;
    }
  • Hi Marcelo, thanks for the help, this way when clicking on another component the alert is displayed once, when clicking a second time on another component the alert is not displayed more :(

0


After some tests it was noted that the event Leave was not suitable for this situation. The alert was displayed every time this event took place and also because the default value was equal to null, that is, when starting to focus it was assigned to the Target component however because of its default value Null the alert was displayed causing the event Leave was triggered again causing the terrible loop...

The solution was to change the event of Leave for Validating because in this way it was possible to validate the value of the Target component even if the value is null, subsequently displayed the alert only once. In matter of usability of the system it was better to change the function Focus() for ShowPopup().


Below follows the updated code fragment:

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...
    }
}

Browser other questions tagged

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