您好,欢迎访问一九零五行业门户网

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# 容器上控件排序的内容。
其它类似信息

推荐信息