C# 容器上控件排序

public static class Sort    {        #region 设置PanelControl上按钮显示位置        ///         /// 设置按钮显示位置        ///         /// 需要调整按钮顺序的Panel        /// 按钮间隔        public static void SetButtonCenter(ScrollableControl targetPanel, int buttonSpace)        {            int length = 0;            int maxHeight = 0;            List listBtn = new List();            System.Windows.Forms.Control.ControlCollection c = targetPanel.Controls;            foreach (Control btn in c)            {                listBtn.Add(btn);                length += btn.Width + buttonSpace;                if (maxHeight  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上按钮显示位置        ///         /// 设置按钮显示位置        ///         /// 需要调整按钮顺序的容器控件        /// 按钮间隔        public static void SetButtonCenter(Control container, int buttonSpace)        {            int length = 0;            int maxHeight = 0;            List listControl = new List();            System.Windows.Forms.Control.ControlCollection c = container.Controls;            foreach (Control btn in c)            {                listControl.Add(btn);                length += btn.Width + buttonSpace;                if (maxHeight  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    {        #region IComparer

sort类完善版本(修正传入控件集合大小不一致,排序后文本显示问题)

 public static class Sort    {        #region 设置PanelControl上按钮显示位置        ///         /// 设置按钮显示位置        ///         /// 需要调整按钮顺序的Panel        /// 按钮间隔        public static void SetButtonCenter(ScrollableControl targetPanel, int buttonSpace)        {            int length = 0;            int maxHeight = 0;            bool controlsHeightSame = true;//控件高度是否一致            List lisControl = new List();            System.Windows.Forms.Control.ControlCollection controls = targetPanel.Controls;            foreach (Control btn in controls)            {                lisControl.Add(btn);                length += btn.Width + buttonSpace;                if (maxHeight             {                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上按钮显示位置        ///         /// 设置按钮显示位置        ///         /// 需要调整按钮顺序的容器控件        /// 按钮间隔        public static void SetButtonCenter(Control container, int buttonSpace)        {            int length = 0;            int maxHeight = 0;            bool controlsHeightSame = true;//控件高度是否一致            List listControl = new List();            System.Windows.Forms.Control.ControlCollection c = container.Controls;            foreach (Control btn in c)            {                listControl.Add(btn);                length += btn.Width + buttonSpace;                if (maxHeight             {                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# 容器控件排序的内容,更多相关内容请关注PHP中文网(www.php.cn)!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1432296.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 06:27:03
下一篇 2025年12月17日 06:27:10

相关推荐

发表回复

登录后才能评论
关注微信