C# ListBox.DataSource不在列表上显示数据
伙计们,我有一个点列表列表点=新列表,我想做的是从列表中删除一些数据。我用一个列表框看看它是否有效。这是我的代码:C# ListBox.DataSource不在列表上显示数据,c#,list,for-loop,listbox,C#,List,For Loop,Listbox,伙计们,我有一个点列表列表点=新列表,我想做的是从列表中删除一些数据。我用一个列表框看看它是否有效。这是我的代码: 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 Po
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;
当我删除从列表中删除数据的代码时,列表中的元素将显示在列表框中。帮助大家可能是您在ifdifference[i]==0中使用了错误的变量吗 当使用您的解决方案时,我在测线上得到ArgumentOutOfRangeException.RemoveAtj 当ifdifference[i]==0行更改为:
if(difference[j] == 0)
然后代码执行成功,listbox2中有3项
编辑:
正如@wdosanjos所提到的,您应该使用一个来执行循环。请注意,您正在修改与迭代相同的列表。这可能很容易删除所有点。您最好为应删除的点创建一个新列表:
//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();
是否要删除重复项?使用points=points.Distinct.ToList;。此外,您的sumofXandY代码可以这样重写:var sumofXandY=points.Selectp=>p.X+p.Y;如果差异[i]==0,是否使用了错误的变量?我在使用“I”时会遇到一个例外,但在使用“j”时却没有例外。你能更好地解释一下你到底想做什么吗?删除项目的标准是什么?删除重复的项目?@HighCore是的,我正在尝试删除重复的项目。如果列表中的数据只与差异点相近,因为在我的项目中,它仍然被认为是一个复制品。尽管它只是在点上有所不同。
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 (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();