这是为什么阵列名单中除名code失败? [英] Why is this array list removal code failing?

查看:256
本文介绍了这是为什么阵列名单中除名code失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过传递相应的文字,在列表框中寻找它,并删除该项目从列表框中删除特定的值。有了这个code:

 私人无效UpdateGUIAfterTableSend(字符串listboxVal)
{
    ExceptionLoggingService.Instance.WriteLog(达到frmMain.UpdateGUIAfterTableSend);
    尝试
    {
        listBoxWork.DataSource = NULL; //< =似乎有必要将它设置为空,以规避值没有在预期范围内
        的for(int i = listBoxWork.Items.Count - 1; I> = 0; --i)
        {
            如果(listBoxWork.Items [I]的ToString()。包含(listboxVal))
            {
                listBoxWork.Items.RemoveAt(ⅰ);
            }
        }
    }
    赶上(异常前)
    {
        字符串msgInnerExAndStackTrace =的String.Format({0};内蒙古例如:{1};堆栈跟踪:{2},ex.Message,ex.InnerException,ex.StackTrace);
        ExceptionLoggingService.Instance.WriteLog(的String.Format(从frmMain.UpdateGUIAfterTableSend:{0},msgInnerExAndStackTrace));
    }
}

...虽然,失败;这个问题被记录为:

 日期:2015年2月10日下午1时48分07秒
消息:达到frmMain.UpdateGUIAfterTableSend日期:2015年2月10日下午1时48分07秒
消息:从应用程序范围的异常处理信息:System.InvalidOperationException:InvalidOperationException异常
   在System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
   在HHS.frmMain.SendDeliveries()
   在HHS.frmMain.menuItemSEND_Deliveries_Click(对象发件人,EventArgs的发送)
   在System.Windows.Forms.MenuItem.OnClick(EventArgs的发送)
   在System.Windows.Forms.Menu.ProcessMnuProc(控制ctlThis,WM WM,的Int32的wParam,lParam中的Int32)
   在System.Windows.Forms.Form.WnProc(WM WM,的Int32的wParam,lParam中的Int32)
   在System.Windows.Forms.Control._InternalWnProc(WM WM,的Int32的wParam,lParam中的Int32)
   在Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr的hwnMain)
   在System.Windows.Forms.Application.Run(表格FM)
   在HHS.Program.Main()

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

该列表框通过设置其DataSource像这样填充:

  listBoxWork.DataSource =工作台;

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

 列表<串GT;工作台= hhsdbutils.GetWorkTableNames();

我很困惑它既是什么原因造成的问题(及其溶液)和为什么在UpdateGUIAfterTableSend()catch块未在记录正被抛出的异常。

更新

在史蒂夫的评论,我注释掉了数据源的设置为null。那么例外是:

 日期:2015年2月10日下午二时19分58秒
消息:从frmMain.UpdateGUIAfterTableSend:值没有在预期的范围内;内蒙古例如:;堆栈跟踪:在System.Windows.Forms.ListBox._VerifyNoDataSource()
   在System.Windows.Forms.ListBox.ObjectCollection.RemoveAt(的Int32指数)
   在HHS.frmMain.UpdateGUIAfterTableSend(字符串listboxVal)

更新2

有关阿迪·莱斯特:

 私人无效SendDeliveries()
{
    ExceptionLoggingService .Instance.WriteLog(达到frmMain.SendDeliveries);
    光标诅咒= Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    尝试
    {
        布尔firstRecord = FALSE;
        布尔lastRecord = FALSE;
        的foreach(在listBoxWork.Items字符串tblname)
        {
            //忽略INV表
            如果(tblname.IndexOf(INV)== 0)继续;
            字符串tblSiteNum = hhsdbutils.GetSiteNumForTableName(tblname);
            字符串文件名= HHSUtils.GetGeneratedDSDFileName(tblSiteNum);
            字符串XMLDATA = hhsdbutils.GetDSDDataAsXMLFromTable(tblname,文件名);
            字符串URI =的String.Format({0}交付/ sendXML /鸭嘴/鸭嘴兽/ {1},HHSConsts.BASE_REST_URL,文件名);
            fileXferImp = HHSConsts.GetFileTransferMethodology();
            fileXferImp.SendDataContentsAsXML(URI,XMLDATA,tblname,siteNum,firstRecord,lastRecord);
            串tableRefVal = HHSUtils.GetTableRefValForTableName(tblname,DSD);
            hhsdbutils.DeleteTableReference(tableRefVal,DSD);
            hhsdbutils.DropTable(tblname,tblSiteNum); //< - 将实际只在code创建替换表后这样做
            UpdateGUIAfterTableSend(tblname);
        }
    }
    最后
    {
        Cursor.Current =诅咒;
    }
}


解决方案

在设定ListBox的数据源使用的BindingSource,而不是直接使用列表(串)这是必需的,否则在数据源的任何改变将不被视为由绑定引擎

假设你要像这样设置列表框项目,并希望删除包含字符串的每一项测试:

  listBoxWork =新的ListBox();
清单<串GT;元素=新的List<串GT;()
{
    Test1的,的Test2,Test3的,例1,例题
};BindingSource的BS =新的BindingSource();
bs.DataSource =元素;
listBoxWork.DataSource = BS;

在code,它试图删除的项目使用即时

 私人无效UpdateGUIAfterTableSend(字符串listboxVal)
{
    ExceptionLoggingService.Instance.WriteLog(达到frmMain.UpdateGUIAfterTableSend);
    尝试
    {
        BindingSource的BS = listBoxWork.DataSource为BindingSource的;
        的for(int i = bs.Count - 1; I> = 0; --i)
        {
            如果(BS [I]的ToString()。包含(测试))
            {
                bs.RemoveAt(ⅰ);
            }
        }
    }
    赶上(异常前)
    {
        字符串msgInnerExAndStackTrace =的String.Format({0};内蒙古例如:{1};堆栈跟踪:{2},ex.Message,ex.InnerException,ex.StackTrace);
        ExceptionLoggingService.Instance.WriteLog(的String.Format(从frmMain.UpdateGUIAfterTableSend:{0},msgInnerExAndStackTrace));
    }
}

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()

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 is a list of string populated by a query:

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

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.

UPDATE

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)

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

解决方案

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.

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

这篇关于这是为什么阵列名单中除名code失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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