I am adding custom controls to a FlowLayoutPanel. Each control has a date property. I would like to sort the controls in the flowlayoutpanel based on the date property. I can't presort the controls before I add them because it is possible for the user to add more.
我正在向FlowLayoutPanel添加自定义控件。每个控件都有一个日期属性。我想基于date属性对flowlayoutpanel中的控件进行排序。在添加控件之前,我无法预先控制控件,因为用户可以添加更多控件。
My current thought is when the ControlAdded event for the FlowLayoutPanel is triggered I loop through the controls and use the BringToFront function to order the controls based on the date.
我当前的想法是当触发FlowLayoutPanel的ControlAdded事件时,我遍历控件并使用BringToFront函数根据日期对控件进行排序。
What is the best way to do this?
做这个的最好方式是什么?
2 个解决方案
#1
3
I doubt this is the best but is what I have so far:
我怀疑这是最好的,但我到目前为止:
SortedList<DateTime,Control> sl = new SortedList<DateTime,Control>();
foreach (Control i in mainContent.Controls)
{
if (i.GetType().BaseType == typeof(MyBaseType))
{
MyBaseType iTyped = (MyBaseType)i;
sl.Add(iTyped.Date, iTyped);
}
}
foreach (MyBaseType j in sl.Values)
{
j.SendToBack();
}
#2
0
BringToFront affects the z-order not the x/y position, I suspect you want to sort the FlowLayoutPanel.Controls collection when someone adds or deletes controls in the panel. Probably use SuspendLayout and ResumeLayout around the sorting code.
BringToFront影响z顺序而不影响x / y位置,我怀疑你想在有人添加或删除面板中的控件时对FlowLayoutPanel.Controls集合进行排序。可能在排序代码周围使用SuspendLayout和ResumeLayout。
#1
3
I doubt this is the best but is what I have so far:
我怀疑这是最好的,但我到目前为止:
SortedList<DateTime,Control> sl = new SortedList<DateTime,Control>();
foreach (Control i in mainContent.Controls)
{
if (i.GetType().BaseType == typeof(MyBaseType))
{
MyBaseType iTyped = (MyBaseType)i;
sl.Add(iTyped.Date, iTyped);
}
}
foreach (MyBaseType j in sl.Values)
{
j.SendToBack();
}
#2
0
BringToFront affects the z-order not the x/y position, I suspect you want to sort the FlowLayoutPanel.Controls collection when someone adds or deletes controls in the panel. Probably use SuspendLayout and ResumeLayout around the sorting code.
BringToFront影响z顺序而不影响x / y位置,我怀疑你想在有人添加或删除面板中的控件时对FlowLayoutPanel.Controls集合进行排序。可能在排序代码周围使用SuspendLayout和ResumeLayout。