public static class Sort {#region settings Panelcontrol on the button display location///<summary>///Settings button display Location/ </summary>//<param name= "Targetpanel" > need to adjust the order of the buttons panel</param>//<param name= "but Tonspace "> button interval </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)//Get the maximum altitude {maxheight = btn. Height; }} int pnllength = Targetpanel.width; if (length > Pnllength)//The sum of the length of the button itself is greater than the length of the panel, do not adjust { Return } int startpos = ((pnllength-length)/2); int yPos = 0; if (MaxHeight < targetpanel.height) {YPos = (targetpanel.height-maxheight)/2;//distance panel top The distance of the box} else {yPos = targetpanel.height/10;//distance from the top border of the 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 set control on the display location of the button///<summary>///Set button display location//&L t;/summary>//<param Name= "container" > container controls that need to adjust the order of the buttons </param>//<param name= "Buttonspace > button interval </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)//Get the maximum altitude {maxheight = btn. Height; }} int pnllength = container. Width; if (length > Pnllength)//The total length of the button itself is greater than the length of the panel, do not adjust {return; } int startpos = ((pnllength-length)/2); int yPos = 0; if (MaxHeight < container. Height) {YPos = (container. Height-maxheight)/2;//distance from the top border of the panel} else {YPos = container. height/10;//distance from panel top border} 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> interface: Defines a method that is implemented by a type that compares two objects. public int Compare (Control x, control y) { if (x.tabindex >= y.tabindex) { return 1; } else { return-1; } } #endregion }
Sort class Perfect Version (fix the size of incoming control collection is inconsistent, text display problem after sorting)
public static class Sort {#region settings Panelcontrol on the button display location///<summary>///Settings button display location </summary>//<param name= "Targetpanel" > need to adjust the order of the buttons panel</param>//<param name= "bu Ttonspace "> button interval </param> public static void Setbuttoncenter (ScrollableControl targetpanel, int buttonspace) {int length = 0; int maxheight = 0; BOOL Controlsheightsame = true;//control height is consistent 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)//Get the maximum altitude {maxheight = btn. Height; }}//Determine if the height of the control is consistent//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)//The total length of the button itself is greater than the length of the panel, do not adjust {return; } int startpos = ((pnllength-length)/2); int yPos = 0; int xPos = startpos; Liscontrol.sort (New Buttonsort ()); The start of the control drawing is the top-left vertex, ypos the y-coordinate of the top-left vertex of the control if (controlsheightsame)//The control is the same height {if (maxheight &L T Targetpanel.height) {YPos = (targetpanel.height-maxheight)/2;//distance from panel top border } else {YPos = targetpanel.height/10;//distance from the top border of the panel} foreach (Cont Rol btn in Liscontrol) {btn. Location = new System.Drawing.Point (XPos, YPos); XPos + = btn. Width + buttonspace; }} else//control size is inconsistent, each control's ypos is evaluated separately {foreach (Control btn in Liscontrol) {YPos = (targetpanel.height-btn. Height)/2;//distance btn from the top border of the panel. Location = new System.Drawing.Point (XPos, YPos); XPos + = btn. Width + buttonspace; }}} #endregion #region set control on the display position of the button///<summary>///Set button </summary>//<param Name= "container" > container controls that need to adjust the order of buttons </param>///<param Name= "Buttonspace" > button interval </param> public static void Setbuttoncenter (Control container, intbuttonspace) {int length = 0; int maxheight = 0; BOOL Controlsheightsame = true;//control height is consistent 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)//Get the maximum altitude {maxheight = btn. Height; }}//Determine if the height of the control is consistent//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)//The total length of the button itself is greater than the length of the panel, do not adjust {return; } int startpos = ((pnllength-length)/2); int yPos = 0; int xPos = startpos; Listcontrol.sort (New Buttonsort ()); The start of the control drawing is the top-left vertex, ypos the y-coordinate of the top-left vertex of the control if (controlsheightsame)//The control is the same height {if (maxheight &L T Container. Height) {YPos = (container. Height-maxheight)/2;//distance from the top border of the panel} else {YPos = Co Ntainer. height/10;//distance from panel top border} foreach (Control btn in ListControl) { Btn. Location = new System.Drawing.Point (XPos, YPos); XPos + = btn. Width + buttonspace; }} else//control size is inconsistent, each control's ypos is evaluated separately { foreach (Control btn in ListControl) {YPos = (container. Height-btn. Height)/2;//distance btn from the top border of the panel. Location = new System.Drawing.Point (XPos, YPos); XPos + = btn. Width + buttonspace; }}} #endregion}
The above is the C # container control ordering content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!