为什么清除 ListBox 会导致应用程序崩溃 "Value does not fall within the expected range"?

Why does clearing a ListBox cause an app to crash with "Value does not fall within the expected range"?

我有这段代码可以用 table:

中的值填充列表框
private void PopulateTransactionListBoxWithWorkTables()
{
    ExceptionLoggingService.Instance.WriteLog("Reached frmMain.PopulateTransactionListBoxWithWorkTable#1");
    menuItemSEND_Deliveries.Enabled = false;
    menuItemSEND_Inventories.Enabled = false;
    try
    {
        ExceptionLoggingService.Instance.WriteLog("Reached frmMain.PopulateTransactionListBoxWithWorkTable#2");
        if (listBoxWork.Items.Count > 0) // for some reason, this causes a crash when canceling out of a new delivery form
        {
            ExceptionLoggingService.Instance.WriteLog("Reached frmMain.PopulateTransactionListBoxWithWorkTable#3");
            listBoxWork.Items.Clear();
        }
        ExceptionLoggingService.Instance.WriteLog("Reached frmMain.PopulateTransactionListBoxWithWorkTable#4");
        if (!hhsdbutils.TableExists("WorkTable"))
        {
            String msg = HHSConsts.NO_CURRENT_WORK;
            listBoxWork.Items.Add(msg);
        }
        else
        {
            ExceptionLoggingService.Instance.WriteLog("Reached frmMain.PopulateTransactionListBoxWithWorkTable#5");
            List<String> workTables = hhsdbutils.GetWorkTableNames();
            menuItemTopSend.Enabled = workTables.Count > 0;
            menuItemSEND_Deliveries.Enabled = workTables.Any(p => p.StartsWith("DSD"));
            menuItemSEND_Inventories.Enabled = workTables.Any(p => p.StartsWith("INV"));

            listBoxWork.DataSource = workTables;
            if (listBoxWork.Items.Count > 0)
            {
                listBoxWork.SelectedIndex = 0;
            }
        }
    }
    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.PopulateTransactionListBoxWithWorkTable: {0}", msgInnerExAndStackTrace));
    }
} // populateListBoxWithWorkTableData

它在 FormActivate 上运行。它第一次运行时,当应用程序启动时,它工作正常,我看到了 #1、#2、#4 和 #5 的调试字符串。

第二次运行上面的方法——打开,然后关闭另一个表单,列表框中有一个项目——它在点击点 1、2 和 3 后崩溃并出现上述 "Value does not fall within the expected range" .

根据到达的点,似乎 "listBoxWork.Items.Clear();" 是有问题的线,因为那是第一次没有被击中的线。为什么会这样?为什么清除包含一项的列表框会使应用程序崩溃?

更新

从这里更改我的代码:

if (listBoxWork.Items.Count > 0) // for some reason, this causes a crash when canceling out of a new delivery form
{
    ExceptionLoggingService.Instance.WriteLog("Reached frmMain.PopulateTransactionListBoxWithWorkTable#3");
    listBoxWork.Items.Clear();
}

...为此:

if (listBoxWork.Items.Count > 0) // for some reason, this causes a crash when canceling out of a new delivery form
{
    ExceptionLoggingService.Instance.WriteLog("Reached frmMain.PopulateTransactionListBoxWithWorkTable#3");
    listBoxWork = null;
}

...摆脱了 "Value Does Not Fall Within the Expected Oven" 错误,但将其替换为 NRE。

您有使用数据源的 listBoxWork 控件,因此您可能无法直接操作项目集合。尝试将其设置为空:

listBoxWork.DataSource = null;