0

我有一个带有 3 个 XtraTabPages 的 DevExpress XtraTabControl。我正在尝试根据条件删除标签页,在最后一次迭代中删除后,出现错误。

我的代码是

foreach (DevExpress.XtraTab.XtraTabPage ptp in tabContactsDetails.TabPages)
            {
                if (tabContactsDetails.TabPages.Contains(ptp))
                {
                    if (ptp.Name == "tabPTP")
                    {
                        if (maxid == String.Empty || maxid == null || maxid == "lblHiddenDebtorID")
                        {
                            tabContactsDetails.TabPages.Remove(ptp);                                
                        }
                        else
                        {

                        }
                    }
                }
            }

我收到一个错误,比如

Collection was modified; enumeration operation may not execute.
4

1 回答 1

1

迭代时不能更改集合!

我要做的是:

List<XtraTabPage> tabPagesToBeRemoved = new List<XtraTabPage>();

foreach (XtraTabPage ptp in tabContactsDetails.TabPages)
{
    if (shouldBeRemoved())
    {
        tabPagesToBeRemoved.Add(ptp);
    }
}

foreach (XtraTabPage pageToBeRemoved in tabPagesToBeRemoved)
{
    tabContactsDetails.TabPages.Remove(pageToBeRemoved);
}
于 2013-02-14T09:26:24.597 回答