【发布时间】:2021-12-12 23:54:09
【问题描述】:
我在这样的表单上创建了控件列表:
List<Control> list = new List<Control>();
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(Label))
{
list.Add(c);
}
}
这个列表中的所有控件都是标签,所以我需要对这个Controls列表进行升序排序,所以我使用List类的Sort方法如下:
list.Sort();
但它告诉我System.InvalidOperationException: 'Failed to compare two elements in the array.' ArgumentException: At least one object must implement IComparable.
由于我想使用TabIndex 值或至少它的Name 对其进行排序,所以我不清楚。我应该将什么传递给Sort 方法,或者我应该使用什么来代替这个方法?
【问题讨论】:
-
var labels = this.Controls.OfType<Label>().OrderBy(L => L.TabIndex);
标签: c# list winforms sorting controls