为什么这个数组列表删除代码失败? [英] Why is this array list removal code failing?

查看:43
本文介绍了为什么这个数组列表删除代码失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to remove specific values from a listbox by passing the corresponding text, looking for it in the listbox, and removing that item. With this code:

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

...不过,它失败了;问题记录为:

...though, it fails; the problem is logged as:

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 块

Note that it's the "application-wide exception handler" that logs the exception, not the catch block in UpdateGUIAfterTableSend()

列表框是通过像这样设置它的数据源来填充的:

The list box is populated by setting its datasource like so:

listBoxWork.DataSource = workTables;

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

workTables is a list of string populated by a query:

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

我很困惑是什么导致了问题(及其解决方案),以及为什么 UpdateGUIAfterTableSend() 中的 catch 块没有记录抛出的异常.

I'm confused both as to what is causing the problem (and its solution) and why the catch block in UpdateGUIAfterTableSend() is not logging the exception that is being thrown.

在史蒂夫发表评论后,我将数据源的设置取消注释为空.例外是:

After Steve's comment, I uncommented the setting of the datasource to null. the exception then is:

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

对于阿迪莱斯特:

UPDATE 2

For 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;
    }
}

推荐答案

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

When setting the DataSource of the ListBox use a BindingSource instead of directly using the List(Of String) This is required otherwise any change in the DataSource will not be seen by the binding engine.

假设你想用这样的东西来设置你的列表框项目并且想要删除包含字符串Test"的每个项目:

Supposing that you want to set your listbox items with something like this and want to remove every item that contains the string "Test":

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

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

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

Now in your code that tries to remove the items use

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

这篇关于为什么这个数组列表删除代码失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆