簡體   English   中英

ListBox.DataSource不在列表上顯示數據

[英]ListBox.DataSource doesn't show data on the list

伙計們,我有一個積分列表積分= new List(); ,我正在嘗試從列表中刪除一些數據。 我使用一個列表框來查看它是否有效。 這是我的代碼:

        points.Add(new PointF(50, 100));
        points.Add(new PointF(50, 100));
        points.Add(new PointF(200, 300));
        points.Add(new PointF(100, 200 ));
        points.Add(new PointF(50, 100));
        points.Add(new PointF(100, 200));
        points.Add(new PointF(200, 300));
        points.Add(new PointF(100, 200));
        points.Add(new PointF(200, 300));

        listBox1.DataSource = points;

        float[] sumofxandy = new float[points.Count()];
        for (int x = 0; x < points.Count(); x++)
        {
            sumofxandy[x] = points.ElementAt(x).X + points.ElementAt(x).Y;
        }


        //code that removes data from list starts from here
        float[] difference = new float[points.Count()]; //there is something wrong with this and I don't know what. It has no error but it doesn't make my list to be shown in the listbox.
        for (int i = 0; i <= points.Count(); i++) 
        {
            for (int j = 1; j <= points.Count(); j++) 
            {

                difference[j] = sumofxandy[i] - sumofxandy[j];
                if (difference[i] == 0) 
                {
                    points.RemoveAt(j);

                    MessageBox.Show("removed");
                }

            }
        } // ends here

        listBox2.DataSource = points;

當我刪除從列表中刪除數據的代碼時,列表中的元素將顯示在列表框中。 伙計們

可能是您在if(difference[i] == 0)中使用了錯誤的變量嗎?

當使用您的解決方案時,我在行points.RemoveAt(j);收到ArgumentOutOfRangeException points.RemoveAt(j);

difference[j] = sumofxandy[i] - sumofxandy[j];
if (difference[i] == 0) 
{
    points.RemoveAt(j);
     MessageBox.Show("removed");
}

當行if(difference[i] == 0)更改為:

if(difference[j] == 0) 

然后代碼成功執行,listbox2中有3個項目。

編輯:

正如@wdosanjos所提到的,您應該使用<而不是<=循環,因為您是從零開始計數,而不是從1開始。最后一個元素實際上位於Count-1位置,因為列表的索引為零。

編輯:

當我將循環更改為:(注意將<=更改為< ,它不會引發異常。)

for (int i = 0; i < points.Count(); i++)
        {
            for (int j = 1; j < points.Count(); j++)
            {

                difference[j] = sumofxandy[i] - sumofxandy[j];
                if (difference[i] == 0)
                {
                    points.RemoveAt(j);

                    MessageBox.Show("removed");
                 }

            }
        } // ends here

這將返回兩個元素。

嘗試如下更改for循環(從<=< )。 我認為您正在獲取隱藏的IndexOutOfBoundsException

for (int i = 0; i < points.Count(); i++) 
{
    for (int j = 1; j < points.Count(); j++) 

請注意,您正在修改的列表完全相同。 這可能很容易刪除所有要點。 您最好為應該刪除的點創建一個新列表:

    //code that removes data from list starts from here
    List<PointF> pointsToRemove = new List<PointF>();
    float[] difference = new float[points.Count()];
    for (int i = 0; i <= points.Count(); i++) 
    {
        for (int j = 1; j <= points.Count(); j++) 
        {

            difference[j] = sumofxandy[i] - sumofxandy[j];
            if (difference[i] != 0) 
            {
                pointsToRemove.Add(points[j]);

                MessageBox.Show("removed");
            }

        }
    }

    listBox2.DataSource = points.Except(pointsToRemove).ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM