为什么此数组列表删除代码失败?

时间:2015-02-10 19:14:50

标签: c# arraylist invalidoperationexception

我正在尝试通过传递相应的文本,在列表框中查找并删除该项来从列表框中删除特定值。使用此代码:

private void UpdateGUIAfterTableSend(String listboxVal)
{
    ExceptionLoggingService.Instance.WriteLog("Reached frmMain.UpdateGUIAfterTableSend");
    try
    {
        listBoxWork.DataSource = null; // <= It seems necessary to set this to null to circumvent "Value does not fall within the expected range"
        for (int i = listBoxWork.Items.Count - 1; i >= 0; --i)
        {
            if (listBoxWork.Items[i].ToString().Contains(listboxVal))
            {
                listBoxWork.Items.RemoveAt(i);
            }
        }
    }
    catch (Exception ex)
    {
        String msgInnerExAndStackTrace = String.Format("{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message, ex.InnerException, ex.StackTrace);
        ExceptionLoggingService.Instance.WriteLog(String.Format("From frmMain.UpdateGUIAfterTableSend: {0}", msgInnerExAndStackTrace));
    }
}

......但是,它失败了;问题记录为:

Date: 2/10/2015 1:48:07 PM
Message: Reached frmMain.UpdateGUIAfterTableSend

Date: 2/10/2015 1:48:07 PM
Message: From application-wide exception handler: System.InvalidOperationException: InvalidOperationException
   at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
   at HHS.frmMain.SendDeliveries()
   at HHS.frmMain.menuItemSEND_Deliveries_Click(Object sender, EventArgs e)
   at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.Menu.ProcessMnuProc(Control ctlThis, WM wm, Int32 wParam, Int32 lParam)
   at System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam)
   at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
   at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
   at System.Windows.Forms.Application.Run(Form fm)
   at HHS.Program.Main()

请注意,它是记录异常的“应用程序范围的异常处理程序”,而不是UpdateGUIAfterTableSend()中的catch块

通过设置数据源来填充列表框,如下所示:

listBoxWork.DataSource = workTables;

workTables是一个由查询填充的字符串列表:

List<String> workTables = hhsdbutils.GetWorkTableNames();

我对导致问题的原因(及其解决方案)以及为什么UpdateGUIAfterTableSend()中的catch块没有记录正在抛出的异常感到困惑。

更新

在Steve的评论之后,我将数据源的设置取消注释为null。例外是:

Date: 2/10/2015 2:19:58 PM
Message: From frmMain.UpdateGUIAfterTableSend: Value does not fall within the expected range.; Inner Ex: ; Stack Trace:    at System.Windows.Forms.ListBox._VerifyNoDataSource()
   at System.Windows.Forms.ListBox.ObjectCollection.RemoveAt(Int32 index)
   at HHS.frmMain.UpdateGUIAfterTableSend(String listboxVal)

更新2

Adi Lester:

private void SendDeliveries()
{
    ExceptionLoggingService .Instance.WriteLog("Reached frmMain.SendDeliveries");
    Cursor curse = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    try
    {
        bool firstRecord = false;
        bool lastRecord = false;
        foreach (String tblname in listBoxWork.Items)
        {
            // Ignore INV tables
            if (tblname.IndexOf("INV") == 0) continue; 
            String tblSiteNum = hhsdbutils.GetSiteNumForTableName(tblname);
            String fileName = HHSUtils.GetGeneratedDSDFileName(tblSiteNum);
            String xmlData = hhsdbutils.GetDSDDataAsXMLFromTable(tblname, fileName);
            String uri = String.Format("{0}delivery/sendXML/duckbill/platypus/{1}", HHSConsts.BASE_REST_URL, fileName);
            fileXferImp = HHSConsts.GetFileTransferMethodology();
            fileXferImp.SendDataContentsAsXML(uri, xmlData, tblname, siteNum, firstRecord, lastRecord);
            String tableRefVal = HHSUtils.GetTableRefValForTableName(tblname, "DSD");
            hhsdbutils.DeleteTableReference(tableRefVal, "DSD");
            hhsdbutils.DropTable(tblname, tblSiteNum); // <- Will actually do this only after creating replacement tables in code
            UpdateGUIAfterTableSend(tblname);
        }
    }
    finally
    {
        Cursor.Current = curse;
    }
}

3 个答案:

答案 0 :(得分:2)

你的两种做法都是错误的。我的意思是将DataSource设置为null,并直接从ListBox删除项目。

当ListBox是数据绑定的(即DataSource设置)时,您无法直接修改Items集合。您必须删除基础数据源中的项目。数据源应通知数据绑定引擎有关项目的删除,以便ListBox项目同步。

为此,您的DataSource应支持更改通知。如果是WPF,请使用ObservableCollection。如果是winforms,请使用BindingList<T>

所以你的代码应该是这样的:

for (int i = yourDataSource.Count - 1; i >= 0; --i)
{
    if (yourDataSource[i].ToString().Contains(listboxVal))
    {
        yourDataSource.RemoveAt(i);
    }
}

答案 1 :(得分:1)

虽然您没有提供抛出异常的实际代码,但很明显您正在从正在枚举的列表中删除一个项目(可能是通过foreach)。如果更改集合,标准集合将使任何枚举器无效。在for循环中删除不是问题,这就是为什么你不在那里捕获异常的原因。看起来这个方法是在foreach循环中的另一个方法中调用的。

答案 2 :(得分:1)

设置ListBox的DataSource时,使用BindingSource而不是直接使用List(Of String)这是必需的,否则绑定引擎将无法看到DataSource中的任何更改。

假设您要使用类似的内容设置列表框项目,并希望删除包含字符串的所有项目&#34;测试&#34;:

listBoxWork = new ListBox();
List<string> elements = new List<string>()
{
    "Test1", "Test2", "Test3", "Example1", "Example2"
};

BindingSource bs = new BindingSource();
bs.DataSource = elements;
listBoxWork.DataSource = bs;

现在,在您的代码中尝试删除项目使用

private void UpdateGUIAfterTableSend(String listboxVal)
{
    ExceptionLoggingService.Instance.WriteLog("Reached frmMain.UpdateGUIAfterTableSend");
    try
    {
        BindingSource bs = listBoxWork.DataSource as BindingSource;
        for (int i = bs.Count - 1; i >= 0; --i)
        {
            if (bs[i].ToString().Contains("Test"))
            {
                bs.RemoveAt(i);
            }
        }
    }
    catch (Exception ex)
    {
        String msgInnerExAndStackTrace = String.Format("{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message, ex.InnerException, ex.StackTrace);
        ExceptionLoggingService.Instance.WriteLog(String.Format("From frmMain.UpdateGUIAfterTableSend: {0}", msgInnerExAndStackTrace));
    }
}