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