C# 容器上控制項排序

來源:互聯網
上載者:User
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    }


以上就是C# 容器上控制項排序的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.