C# 容器上控件排序
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# 容器上控件排序的内容,更多相关内容请关注PHP中文网(www.php.cn)!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
1 周前
By DDD
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
在哪里可以找到原子中的起重机控制钥匙卡
1 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

使用 C# 的 Active Directory 指南。在这里,我们讨论 Active Directory 在 C# 中的介绍和工作原理以及语法和示例。
