繁体   English   中英

如何从列表框中删除多个项目?

[英]How can I remove more than one item from a listbox?

真的很烦人。 或者说实话,回过头来变得很烦人。

以编程方式从列表框中删除项目应该非常简单,但似乎我尝试的所有操作最终都一样:例外。 这次是“ 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

是的,似乎/应该很简单。 然而,它将仅与第一个合作,然后在出现异常的情况下崩溃。 如果我不使用数据绑定,是否会变得不那么坦率?在填充列表框时,手动一对一地手动添加值?

UPDATE

是的,将要删除的项目保存在列表中,然后在事实起作用后立即将其全部删除。 我将代码更改为此:

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);
        }
    }
    . . .

...现在工作正常。

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

您试图在要同时更新的数据绑定控件上使用枚举数。 对此不会很高兴。 完成循环后,将要更改的所有内容保存到数据绑定控件中,然后对已保存的每个更改尝试更新gui方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM