【问题标题】:How can I remove more than one item from a listbox?如何从列表框中删除多个项目?
【发布时间】:2015-04-13 23:07:17
【问题描述】:

真的越来越烦人了;或者,老实说,前一阵子它变得很烦人。

以编程方式从列表框中删除项目应该非常简单,但似乎我尝试的所有操作都以相同的方式结束:有一个例外。这次是“InvalidOperationException”。在上下文中(日志文件摘录):

Date: 2/12/2015 7:15:17 PM
Message: Reached frmMain.UpdateGUIAfterTableSend

Date: 2/12/2015 7:15:17 PM
Message: From frmMain.SendDeliveries(): InvalidOperationException; Inner Ex: ; Stack Trace:    at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
   at HHS.frmMain.SendDeliveries()

我通过查询表来填充列表框,并使用查询结果填充字符串列表。然后我将该字符串列表指定为列表框的数据源。填充工作正常;是人口减少给了我狂热。

这是代码。对于这个问题,SendDeliveries() 调用的关键是 UpdateGUIAfterTableSend()

private void SendDeliveries()
{
    ExceptionLoggingService .Instance.WriteLog("Reached
frmMain.SendDeliveries");
    Cursor curse = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    try
    {
        bool firstRecord = false;
        bool lastRecord = false;
        try
        {
            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);
                // Verify that "delivery" is the correct val in this URL
                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);
                hhsdbutils.DeleteTableReference(tblname);
                hhsdbutils.DropTable(tblname, tblSiteNum);
                UpdateGUIAfterTableSend(tblname);
            }
        }
        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.SendDeliveries(): {0}", msgInnerExAndStackTrace));
        }
    }
    finally
    {
        Cursor.Current = curse;
    }
}

private void UpdateGUIAfterTableSend(String listboxVal)
{
    ExceptionLoggingService.Instance.WriteLog("Reached
frmMain.UpdateGUIAfterTableSend");
    try
    {
        BindingSource bs = listBoxWork.DataSource as BindingSource;
        List<string> values = bs.DataSource as List<string>;
        values.RemoveAll(v => v.Contains(listboxVal));
        bs.ResetBindings(false);
    }
    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("Fro
frmMain.UpdateGUIAfterTableSend: {0}", msgInnerExAndStackTrace));
    }
}

即使列表框中有多个项目,也只会删除一个,因为它会因 InvalidOperationException 而崩溃。日志文件表明在 SendDeliveries() 中引发了异常,但我不明白为什么会出现问题。

如果发送了三个表会发生什么:

Send the first one, and remove the listbox item that represents it to the user from the listbox
Send the second one, and remove the listbox item that represents it to the user from the listbox
Send the third one, and remove the listbox item that represents it to the user from the listbox

是的,看起来/应该很简单。然而它只会与第一个合作,然后因该异常而崩溃。如果我不使用数据绑定——只是在填充列表框时手动一个一个地添加值,会不会那么烦躁?

更新

是的,将要删除的项目保存在一个列表中,然后在事实生效后立即将它们全部删除。我将代码更改为:

private void SendDeliveries()
{
    List<String> tableNames = new List<string>();
    try
    {
        try
        {
            foreach (String tblname in listBoxWork.Items)
            {
                String tblSiteNum 
    hhsdbutils.GetSiteNumForTableName(tblname);
                . . .
                tableNames.Add(tblname);
            }
            UpdateGUIAfterTableSend(tableNames);
        }
        . . .

private void UpdateGUIAfterTableSend(IEnumerable<String> listboxVals)
{
    try
    {
        BindingSource bs = listBoxWork.DataSource as BindingSource;
        if (bs != null)
        {
            List<string> values = bs.DataSource as List<string>;
            foreach (String listboxVal in listboxVals)
            {
                if (values != null)
                {
                    values.RemoveAll(v => v.Contains(listboxVal));
                }
            }
        }
        if (null != bs)
        {
            bs.ResetBindings(false);
        }
    }
    . . .

...现在可以正常使用了。

【问题讨论】:

  • 您正在遍历项目列表并同时对其进行修改 - 这不起作用

标签: c# listbox datasource windows-ce invalidoperationexception


【解决方案1】:

不完全确定,但我会尝试在循环之外的 GUI 中创建您想要更新的项目列表,然后使用您想要修改的项目列表更新 GUI。

您正尝试在同时更新的数据绑定控件上使用枚举器。它不会对此感到非常高兴。完成循环后,保存您想要对数据绑定控件进行的所有更改,然后尝试更新您保存的每个更改的 gui 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2010-11-29
    相关资源
    最近更新 更多