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)!

要开始C#.NET开发,你需要:1.了解C#的基础知识和.NET框架的核心概念;2.掌握变量、数据类型、控制结构、函数和类的基本概念;3.学习C#的高级特性,如LINQ和异步编程;4.熟悉常见错误的调试技巧和性能优化方法。通过这些步骤,你可以逐步深入C#.NET的世界,并编写高效的应用程序。

C#和.NET的关系是密不可分的,但它们不是一回事。C#是一门编程语言,而.NET是一个开发平台。C#用于编写代码,编译成.NET的中间语言(IL),由.NET运行时(CLR)执行。

C#.NET依然重要,因为它提供了强大的工具和库,支持多种应用开发。1)C#结合.NET框架,使开发高效便捷。2)C#的类型安全和垃圾回收机制增强了其优势。3).NET提供跨平台运行环境和丰富的API,提升了开发灵活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#和.NET通过不断的更新和优化,适应了新兴技术的需求。1)C#9.0和.NET5引入了记录类型和性能优化。2).NETCore增强了云原生和容器化支持。3)ASP.NETCore与现代Web技术集成。4)ML.NET支持机器学习和人工智能。5)异步编程和最佳实践提升了性能。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的编程过程包括以下步骤:1)编写C#代码,2)编译为中间语言(IL),3)由.NET运行时(CLR)执行。C#在.NET中的优势在于其现代化语法、强大的类型系统和与.NET框架的紧密集成,适用于从桌面应用到Web服务的各种开发场景。

C#是一种现代、面向对象的编程语言,由微软开发并作为.NET框架的一部分。1.C#支持面向对象编程(OOP),包括封装、继承和多态。2.C#中的异步编程通过async和await关键字实现,提高应用的响应性。3.使用LINQ可以简洁地处理数据集合。4.常见错误包括空引用异常和索引超出范围异常,调试技巧包括使用调试器和异常处理。5.性能优化包括使用StringBuilder和避免不必要的装箱和拆箱。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

WebStorm Mac版
好用的JavaScript开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载
最流行的的开源编辑器