winfrom的panel里面的控件怎么按Y轴的上下顺序遍历。

时间:2021-10-06 00:59:03
panel里面的控件是可以随意拖动的,点击panel外面的按钮,功能是把panel里面的控件按照Y轴从上到下的顺序给遍历出来。。请问有什么好的方法没有??本来想用 Control.ControlCollection集合弄个冒泡排序,,但是 Control.ControlCollection[i]是只读的,无法改变。没招了,有人做过类似的没有??

4 个解决方案

#1


遍历panel的Controls 按着子控件的location的top值排序。

#2


排序进一个新的list<Control>不就好了

#3


  List<Control> listStr = new List<Control>();
            foreach (Control ctrl in this.flowLayoutPanel1.Controls)
            {
                listStr.Add(ctrl);
            }

            Control c = null;

            for (int i = 0; i < listStr.Count; i++)
            {
                for (int j = i + 1; j < listStr.Count; j++)
                {
                    if (listStr[j].Location.Y < listStr[i].Location.Y)
                    {
                        c = listStr[j];
                        listStr[j] = listStr[i];
                        listStr[i] = c;
                    }
                }
            }
            foreach (Control ctrl in listStr)
            {
                MessageBox.Show(ctrl.Location.Y.ToString());
            }

总算搞定了。。!

#4


引用 2 楼 xdashewan 的回复:
排序进一个新的list<Control>不就好了

谢谢给思路。!

#1


遍历panel的Controls 按着子控件的location的top值排序。

#2


排序进一个新的list<Control>不就好了

#3


  List<Control> listStr = new List<Control>();
            foreach (Control ctrl in this.flowLayoutPanel1.Controls)
            {
                listStr.Add(ctrl);
            }

            Control c = null;

            for (int i = 0; i < listStr.Count; i++)
            {
                for (int j = i + 1; j < listStr.Count; j++)
                {
                    if (listStr[j].Location.Y < listStr[i].Location.Y)
                    {
                        c = listStr[j];
                        listStr[j] = listStr[i];
                        listStr[i] = c;
                    }
                }
            }
            foreach (Control ctrl in listStr)
            {
                MessageBox.Show(ctrl.Location.Y.ToString());
            }

总算搞定了。。!

#4


引用 2 楼 xdashewan 的回复:
排序进一个新的list<Control>不就好了

谢谢给思路。!