C# 容器上控制元件排序

衣舞晨風發表於2015-06-17
public static class Sort
    {

        #region 設定PanelControl上按鈕顯示位置
        /// <summary>
        /// 設定按鈕顯示位置
        /// </summary>
        /// <param name="targetPanel">需要調整按鈕順序的Panel</param>
        /// <param name="buttonSpace">按鈕間隔</param>
        public static void SetButtonCenter(ScrollableControl targetPanel, int buttonSpace)
        {
            int length = 0;
            int maxHeight = 0;
            List<Control> listBtn = new List<Control>();
            System.Windows.Forms.Control.ControlCollection c = targetPanel.Controls;
            foreach (Control btn in c)
            {
                listBtn.Add(btn);
                length += btn.Width + buttonSpace;
                if (maxHeight < btn.Height)//獲取最大高度
                {
                    maxHeight = btn.Height;
                }
            }
            int pnlLength = targetPanel.Width;
            if (length > pnlLength) //本身按鈕的長度總和大於了panel的長度,不調整
            {
                return;
            }
            int startPos = ((pnlLength - length) / 2);
            int yPos = 0;
            if (maxHeight < targetPanel.Height)
            {
                yPos = (targetPanel.Height - maxHeight) / 2;//距離panel上邊框的距離
            }
            else
            {
                yPos = targetPanel.Height / 10;//距離panel上邊框的距離
            }
            int xPos = startPos;
            listBtn.Sort(new ButtonSort());
            foreach (Control btn in listBtn)
            {
                btn.Location = new System.Drawing.Point(xPos, yPos);
                xPos += btn.Width + buttonSpace;
            }
        }
        #endregion 

        #region 設定Control上按鈕顯示位置
        /// <summary>
        /// 設定按鈕顯示位置
        /// </summary>
        /// <param name="container">需要調整按鈕順序的容器控制元件</param>
        /// <param name="buttonSpace">按鈕間隔</param>
        public static void SetButtonCenter(Control container, int buttonSpace)
        {
            int length = 0;
            int maxHeight = 0;
            List<Control> listControl = new List<Control>();
            System.Windows.Forms.Control.ControlCollection c = container.Controls;
            foreach (Control btn in c)
            {
                listControl.Add(btn);
                length += btn.Width + buttonSpace;
                if (maxHeight < btn.Height)//獲取最大高度
                {
                    maxHeight = btn.Height;
                }
            }
            int pnlLength = container.Width;
            if (length > pnlLength) //本身按鈕的長度總和大於了panel的長度,不調整
            {
                return;
            }
            int startPos = ((pnlLength - length) / 2);
            int yPos = 0;
            if (maxHeight < container.Height)
            {
                yPos = (container.Height - maxHeight) / 2;//距離panel上邊框的距離
            }
            else
            {
                yPos = container.Height / 10;//距離panel上邊框的距離
            }
            int xPos = startPos;
            listControl.Sort(new ButtonSort());
            foreach (Control btn in listControl)
            {
                btn.Location = new System.Drawing.Point(xPos, yPos);
                xPos += btn.Width + buttonSpace;
            }
        }
        #endregion 


    }


 public class ButtonSort : IComparer<Control>
    {
        #region IComparer<Button> Members
        //IComparer<T> 介面:定義型別為比較兩個物件而實現的方法。
        public int Compare(Control x, Control y)
        {
            if (x.TabIndex >= y.TabIndex)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        #endregion
    }

Sort類完善版本(修正傳入控制元件集合大小不一致,排序後文字顯示問題)

 public static class Sort
    {

        #region 設定PanelControl上按鈕顯示位置
        /// <summary>
        /// 設定按鈕顯示位置
        /// </summary>
        /// <param name="targetPanel">需要調整按鈕順序的Panel</param>
        /// <param name="buttonSpace">按鈕間隔</param>
        public static void SetButtonCenter(ScrollableControl targetPanel, int buttonSpace)
        {
            int length = 0;
            int maxHeight = 0;
            bool controlsHeightSame = true;//控制元件高度是否一致
            List<Control> lisControl = new List<Control>();
            System.Windows.Forms.Control.ControlCollection controls = targetPanel.Controls;
            foreach (Control btn in controls)
            {
                lisControl.Add(btn);
                length += btn.Width + buttonSpace;
                if (maxHeight < btn.Height)//獲取最大高度
                {
                    maxHeight = btn.Height;
                }
            }
            //判斷控制元件高度是否一致
            //lisControl.ForEach(delegate(Control control)
            //{
            //    if (control.Height != maxHeight)
            //    {
            //        controlsHeightSame = false;
            //    }
            //});
            lisControl.ForEach(control =>
            {
                if (control.Height != maxHeight)
                {
                    controlsHeightSame = false;
                }
            });
            int pnlLength = targetPanel.Width;
            if (length > pnlLength) //本身按鈕的長度總和大於了panel的長度,不調整
            {
                return;
            }
            int startPos = ((pnlLength - length) / 2);
            int yPos = 0;
            int xPos = startPos;
            lisControl.Sort(new ButtonSort());
            //控制元件繪製的起點是左上角的頂點,yPos即控制元件的左上角頂點的y座標
            if (controlsHeightSame)//控制元件高度一致
            {
                if (maxHeight < targetPanel.Height)
                {
                    yPos = (targetPanel.Height - maxHeight) / 2;//距離panel上邊框的距離
                }
                else
                {
                    yPos = targetPanel.Height / 10;//距離panel上邊框的距離
                }
                foreach (Control btn in lisControl)
                {
                    btn.Location = new System.Drawing.Point(xPos, yPos);
                    xPos += btn.Width + buttonSpace;
                }
            }
            else//控制元件大小不一致,每個控制元件的yPos單獨計算
            {
                foreach (Control btn in lisControl)
                {
                    yPos = (targetPanel.Height - btn.Height) / 2;//距離panel上邊框的距離
                    btn.Location = new System.Drawing.Point(xPos, yPos);
                    xPos += btn.Width + buttonSpace;
                }
            }

        }
        #endregion

        #region 設定Control上按鈕顯示位置
        /// <summary>
        /// 設定按鈕顯示位置
        /// </summary>
        /// <param name="container">需要調整按鈕順序的容器控制元件</param>
        /// <param name="buttonSpace">按鈕間隔</param>
        public static void SetButtonCenter(Control container, int buttonSpace)
        {
            int length = 0;
            int maxHeight = 0;
            bool controlsHeightSame = true;//控制元件高度是否一致
            List<Control> listControl = new List<Control>();
            System.Windows.Forms.Control.ControlCollection c = container.Controls;
            foreach (Control btn in c)
            {
                listControl.Add(btn);
                length += btn.Width + buttonSpace;
                if (maxHeight < btn.Height)//獲取最大高度
                {
                    maxHeight = btn.Height;
                }
            }
            //判斷控制元件高度是否一致
            //listControl.ForEach(delegate(Control control)
            //{
            //    if (control.Height != maxHeight)
            //    {
            //        controlsHeightSame = false;
            //    }
            //});
            listControl.ForEach(control =>
            {
                if (control.Height != maxHeight)
                {
                    controlsHeightSame = false;
                }
            });
            int pnlLength = container.Width;
            if (length > pnlLength) //本身按鈕的長度總和大於了panel的長度,不調整
            {
                return;
            }
            int startPos = ((pnlLength - length) / 2);
            int yPos = 0;
            int xPos = startPos;
            listControl.Sort(new ButtonSort());
            //控制元件繪製的起點是左上角的頂點,yPos即控制元件的左上角頂點的y座標
            if (controlsHeightSame)//控制元件高度一致
            {
                if (maxHeight < container.Height)
                {
                    yPos = (container.Height - maxHeight) / 2;//距離panel上邊框的距離
                }
                else
                {
                    yPos = container.Height / 10;//距離panel上邊框的距離
                }
                foreach (Control btn in listControl)
                {
                    btn.Location = new System.Drawing.Point(xPos, yPos);
                    xPos += btn.Width + buttonSpace;
                }
            }
            else//控制元件大小不一致,每個控制元件的yPos單獨計算
            {
                foreach (Control btn in listControl)
                {
                    yPos = (container.Height - btn.Height) / 2;//距離panel上邊框的距離
                    btn.Location = new System.Drawing.Point(xPos, yPos);
                    xPos += btn.Width + buttonSpace;
                }
            }

        }
        #endregion


    }


相關文章