Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#listbox fed List的行为非常怪异_C#_List_Listbox_Datasource - Fatal编程技术网

c#listbox fed List的行为非常怪异

c#listbox fed List的行为非常怪异,c#,list,listbox,datasource,C#,List,Listbox,Datasource,好的,我有这个功能,它通过gui中的选择清除列表,并用新选择的。。。它非常简单:一个具有构造函数和重写的ToString方法的类被抛出到一个列表中,并用作listbox的数据源。。。我做的任何测试都是完美无瑕的,但在这个特定的程序中,我不知道为什么。我不是绝望,我只是好奇而已 我发现如果我简单地将列表转换为数组,它就会工作。。。但是为什么呢 这是我的原始代码,不起作用: private void QaControl(string _itemNo, int _curIndex) {

好的,我有这个功能,它通过gui中的选择清除列表,并用新选择的。。。它非常简单:一个具有构造函数和重写的ToString方法的类被抛出到一个列表中,并用作listbox的数据源。。。我做的任何测试都是完美无瑕的,但在这个特定的程序中,我不知道为什么。我不是绝望,我只是好奇而已

我发现如果我简单地将列表转换为数组,它就会工作。。。但是为什么呢

这是我的原始代码,不起作用:

private void QaControl(string _itemNo, int _curIndex)
    {
        List<QaControlPoint> list = new List<QaControlPoint>();

        //remove old ones from list 
        if (listBox1.DataSource != null)
        {
            list = (List<QaControlPoint>)listBox1.DataSource;

            for (int n = listBox1.Items.Count - 1; n >= 0; --n)
            {
                QaControlPoint qcp = (QaControlPoint)listBox1.Items[n];
                if (_boxList.IndexOf(qcp.link_box) >= _curIndex)
                    list.Remove(qcp);
            }
        }

        string fs = service.getQa(Int32.Parse(_itemNo), "R");
        string[] temp = fs.Split('@');
        for (int a = 0; a < temp.Length - 1; a++)
            list.Add(new QaControlPoint(temp[a], _boxList[_curIndex]));
        listBox1.DataSource = list;
    }
private void控制(字符串_itemNo,int _curIndex)
{
列表=新列表();
//从列表中删除旧的
if(listBox1.DataSource!=null)
{
list=(list)listBox1.DataSource;
对于(int n=listBox1.Items.Count-1;n>=0;--n)
{
QaControlPoint qcp=(QaControlPoint)列表框1.项[n];
if(\u-boxList.IndexOf(qcp.link\u-box)>=\u-curIndex)
列表。删除(qcp);
}
}
字符串fs=service.getQa(Int32.Parse(_itemNo),“R”);
字符串[]temp=fs.Split('@');
对于(int a=0;a
这段代码,我使用简单数组作为数据源,非常完美

private void QaControl(string _itemNo, int _curIndex)
    {
        List<QaControlPoint> list1 = new List<QaControlPoint>();

        //remove old ones from list 
        if (listBox1.DataSource != null)
        {
            //convert to list here
            QaControlPoint[] rcp = (QaControlPoint[])listBox1.DataSource;
            list1.AddRange(rcp);

            QaControlPoint rcp2;
            for (int n = listBox1.Items.Count - 1; n >= 0; --n)
            {
                rcp2 = (QaControlPoint)listBox1.Items[n];
                if (_boxList.IndexOf(rcp2.link_box) >= _curIndex)
                    list1.Remove(rcp2);
            }
        }

        string fs = service.getQa(Int32.Parse(_itemNo), "R");
        string[] temp = fs.Split('@');
        for (int a = 0; a < temp.Length - 1; a++)
            list1.Add(new QaControlPoint(temp[a], _boxList[_curIndex]));

        //convert back to array here
        QaControlPoint[] rcnew = list1.ToArray();
        listBox1.DataSource = rcnew;
    }
private void控制(字符串_itemNo,int _curIndex)
{
List list1=新列表();
//从列表中删除旧的
if(listBox1.DataSource!=null)
{
//在此处转换为列表
QaControlPoint[]rcp=(QaControlPoint[])listBox1.DataSource;
列表1.添加范围(rcp);
控制点rcp2;
对于(int n=listBox1.Items.Count-1;n>=0;--n)
{
rcp2=(QaControlPoint)列表框1.项[n];
if(\u boxList.IndexOf(rcp2.link\u box)>=\u curIndex)
清单1.移除(rcp2);
}
}
字符串fs=service.getQa(Int32.Parse(_itemNo),“R”);
字符串[]temp=fs.Split('@');
对于(int a=0;a
这里只是猜测,但在第一个示例中,数据源属性从未更改。当然,您正在添加和删除项,但是当您设置数据源时,您正在将其设置为已经具有的相同对象实例

在第二个示例中,您使用的是一个新实例(既有一个新列表,又有一个新数组)

我的猜测是,ListBox足够聪明,可以识别出,当您设置为同一个实例时,您没有更改数据源的值,因此它没有执行非常昂贵的刷新自己的步骤。这看起来像WinForms,我对它不太熟悉,但我不认为
List
实现了ListBox捕获集合更改事件所需的接口

请尝试以下方法:

    private void QaControl( string _itemNo, int _curIndex ) {
        List<QaControlPoint> list = new List<QaControlPoint>();

        //remove old ones from list         
        if ( listBox1.DataSource != null ) {
            List<QaControlPoint> oldList = (List<QaControlPoint>) listBox1.DataSource;
            list.AddRange( oldList );
            for ( int n = listBox1.Items.Count - 1; n >= 0; --n ) {
                QaControlPoint qcp = (QaControlPoint) listBox1.Items[n];
                if ( _boxList.IndexOf( qcp.link_box ) >= _curIndex )
                    list.Remove( qcp );
            }
        }

        string fs = service.getQa( Int32.Parse( _itemNo ), "R" );
        string[] temp = fs.Split( '@' );
        for ( int a = 0; a < temp.Length - 1; a++ )
            list.Add( new QaControlPoint( temp[a], _boxList[_curIndex] ) );
        listBox1.DataSource = list;
    }
private void控制(字符串_itemNo,int _curIndex){
列表=新列表();
//从列表中删除旧的
if(listBox1.DataSource!=null){
List oldList=(List)listBox1.DataSource;
list.AddRange(oldList);
对于(int n=listBox1.Items.Count-1;n>=0;--n){
QaControlPoint qcp=(QaControlPoint)列表框1.项[n];
if(\u-boxList.IndexOf(qcp.link\u-box)>=\u-curIndex)
列表。删除(qcp);
}
}
字符串fs=service.getQa(Int32.Parse(_itemNo),“R”);
字符串[]temp=fs.Split('@');
对于(int a=0;a
解释你所说的“不工作”-不显示任何项目是什么意思?