开发者

Disposing 2nd Time Doesn't Dispose Form?

开发者 https://www.devze.com 2023-01-07 15:37 出处:网络
I use this method to close forms according to their Id, but if I open a form with Id 2, close it once, it works fine, but if I open a form again with Id 2, it doesn\'t close the form (the form just st

I use this method to close forms according to their Id, but if I open a form with Id 2, close it once, it works fine, but if I open a form again with Id 2, it doesn't close the form (the form just stays on the screen). I have no idea what is going on and desperately need help. Thanks!

public void OutsideClose(long Id)
{
    if (InvokeRequired)
    {
        Invoke(new Action<long>(OutsideClose), Id);
    }
    else
    {
        var FormToClose = ListFo开发者_JS百科rms.Find(a=> a.Id == Id);
        if(FormToClose != null)
        {
            FormToClose.Dispose();
            OpenForms.Remove(Id);
        }
    }
}


Ok, I am dumb! I was disposing the form, but I wasn't removing it from the list. Now I added a line of code to do that and now it's fixed.

Edit: Thanks Jon for making me look closer at the code (especially the two lists)

0

精彩评论

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