Sort controls on C # containers

Source: Internet
Author: User
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)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.