Winform Panel button Position

Source: Internet
Author: User
1. Set a button centered on the panel

This.btnExit.Location = new System.Drawing.Point (PNLBUTTON.WIDTH/2, PNLBUTTON.HEIGHT/2);

2. Automatic sorting of multiple buttons on panel

Original state:


Code to adjust the order:

        <summary>////Set button display location///</summary>//<param name= "Targetpanel" > To set button panel</param>//<param name= "Buttonspace" > The interval between buttons </param> public void Setbuttoncenter (Pa            Nel targetpanel, int buttonspace) {int length = 0;            list<button> listbtn = new list<button> ();            System.Windows.Forms.Control.ControlCollection C = targetpanel.controls;                foreach (Button btn in c) {Listbtn.add (BTN); Length + = btn.            Width + buttonspace;            } 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-10;            Left indent 10 point bit int yPos = TARGETPANEL.HEIGHT/2;            int xPos = startpos; foreach (Button btn in listbtn) {btn. Location = new System.Drawing.Point(XPos, YPos); XPos + = btn.            Width + buttonspace; }        }

The initial adjustment looks like this:

Note that the button order is reversed!

So how do you make the button order right?

The code is as follows:


private void Form1_Load (object sender, EventArgs e) {setbuttoncenter (paneltest, 2); }///<summary>///Set button display location///</summary>//<param name= "Pnlbutton" > Need to adjust Button order panel</param>//<param name= "buttonspace" > button interval </param> public void Setbuttoncenter (            Panel targetpanel,int buttonspace) {int length = 0;            list<button> listbtn = new list<button> ();            System.Windows.Forms.Control.ControlCollection C = targetpanel.controls;                foreach (Button btn in c) {Listbtn.add (BTN); Length + = btn.            Width + buttonspace;            } 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-10;            Left indent 10 point bit int yPos = TARGETPANEL.HEIGHT/2; int xPos = startpos;            Listbtn.sort (New Buttonsort ()); foreach (Button btn in listbtn) {btn.                Location = new System.Drawing.Point (XPos, YPos); XPos + = btn.            Width + buttonspace; }} public class Buttonsort:icomparer<button> {#region icomparer<button> Me            Mbers//icomparer<t> Interface: Defines a method that is implemented by a type that compares two objects. public int Compare (button x, button y) {if (X.tabindex >= y.tabindex) ret                Urn 1;            else return-1; } #endregion}

The effect at this point is as follows:




Expand: Click to open link

The above is the WinForm Panel button location content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.