简体   繁体   中英

ListBox.DataSource doesn't show data on the list

Help guys, I have a List of Points List points = new List(); , what I'm trying to do is removing some data from the list. I used a listbox to see whether it works. This is my code:

        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;

When I erased the code that removes data from the list, the elements inside the list will be shown in the listbox. Help guys

Could it be that you are using the wrong variable in the if(difference[i] == 0) ?

When using your solution I get an ArgumentOutOfRangeException on the line points.RemoveAt(j);

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

When the line if(difference[i] == 0) are changed to:

if(difference[j] == 0) 

then the code executes successful, with 3 items in listbox2.

EDIT:

As @wdosanjos mentions, you should do your loops with a < rather than <= since you start counting at zero, and not 1. The last element is really at position Count - 1, since the list are zero indexed.

EDIT:

When I changed the loops to: (notice the change in <= to < , it does not throw an exception.)

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

This returns two elements.

Try changing your for loops as follows (from <= to < ). I think you are getting a hidden IndexOutOfBoundsException .

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

Notice that you are modifying the very same list you are iterating over. This may just as easy remove all the points. You should better create a new list for points that should be removed:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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