C# 容器上控件排序

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: public static class Sort { #region 设置PanelControl上按钮显示位置 /// <summary> /// 设置按钮显示位置 /// </summary> /// <param name="targetPanel">需要调整按钮
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


    }


相关文章
|
4月前
|
C# 容器
39.c#:groupbox容器
39.c#:groupbox容器
112 1
|
1月前
|
容器
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Group Box的使用及说明
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Group Box的使用及说明
100 3
|
1月前
|
容器
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Tab Widget的使用及说明
【Qt 学习笔记】Qt常用控件 | 容器类控件 | Tab Widget的使用及说明
19 2
|
3月前
|
C++ 容器
C++之deque容器(构造、赋值、大小、插入与删除、存取、排序)
C++之deque容器(构造、赋值、大小、插入与删除、存取、排序)
|
3月前
|
安全 Java 容器
Java 1.8新特性使用记录:Filter、数据容器的转换、排序Sorted
Java 1.8新特性使用记录 有些方法一段时间不使用会忘记,这里要记录一下,方便以后使用 一、过滤Filter 二、数据容器的转换 三、List 排序
30 0
|
4月前
|
容器 内存技术
Qt中常用容器组控件介绍和实操-1
Qt中常用容器组控件介绍和实操
|
4月前
|
API 容器
Qt中常用容器组控件介绍和实操-2
Qt中常用容器组控件介绍和实操
|
4月前
|
存储 C++ 容器
set容器一自定义数据类型指定排序规则讲解
set容器一自定义数据类型指定排序规则讲解
124 1
|
4月前
|
开发框架 .NET C#
C#学习相关系列之Linq常用方法---排序(一)
C#学习相关系列之Linq常用方法---排序(一)
|
4月前
|
C#
C#中sort排序相关用法介绍
C#中sort排序相关用法介绍