开发者

c#, listbox, stackOverflow exception

I inherited KryptonToolkit ListBox control to get SelectedItemChanging event.

public class CPListBox : KryptonListBox
{       
    public event CancelEventHandler SelectedIndexChanging;

    protected virtual void OnSelectedIndexChanging(Ca开发者_如何转开发ncelEventArgs e)
    {
        SelectedIndexChanging(this, e);
    }

    protected override void OnSelectedIndexChanged(EventArgs e)
    {           
        CancelEventArgs cArgs = new CancelEventArgs();
        OnSelectedIndexChanged(cArgs);

        if(!cArgs.Cancel)
        {
            base.OnSelectedIndexChanged(e);
        }
    }
}

In form I handle event with code:

    void UsersListBoxSelectedIndexChanging(object sender, CancelEventArgs e)
    {
        if(_presenter.CurrentUser.IsModified)
        {
            MessageBox.Show("Nie zapisales zmian!");
            e.Cancel = true;
        }

    }

And I got stackOverflow ;) Exception. Maybe someone now what is wron with this code?


You're recursively calling the method in itself forever. There's no terminating condition for these recursive calls. It'll result in Stack Overflow.

protected override void OnSelectedIndexChanged(EventArgs e)
{                       
       CancelEventArgs cArgs = new CancelEventArgs();
       OnSelectedIndexChanged(cArgs); // Clearly calling yourself indefinitely.
       //...
}


protected override void OnSelectedIndexChanged(EventArgs e)
        {                       
                CancelEventArgs cArgs = new CancelEventArgs();
                //Next line!!
                OnSelectedIndexChanged(cArgs);

                if(!cArgs.Cancel)
                {
                        base.OnSelectedIndexChanged(e);
                }
        }

You're calling yourself. Hence the StackOVerflow exception.


You have OnSelectedIndexChanged call within OnSelectedIndexChanged, it's an endless recursive call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜