在頁面中添加Panel容器和Button控制項 這是設計器代碼 namespace WinForm { partial class DefaultForm { /// <summary> /// 必需的設計器變數。 /// </summary> private System.ComponentMode ...
在頁面中添加Panel容器和Button控制項
這是設計器代碼
namespace WinForm { partial class DefaultForm { /// <summary> /// 必需的設計器變數。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設計器生成的代碼 /// <summary> /// 設計器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內容。 /// </summary> private void InitializeComponent() { this.plBaseButtom = new System.Windows.Forms.Panel(); this.btnNew = new System.Windows.Forms.Button(); this.btnSave = new System.Windows.Forms.Button(); this.btnExit = new System.Windows.Forms.Button(); this.plBaseButtom.SuspendLayout(); this.SuspendLayout(); // // plBaseButtom // this.plBaseButtom.Controls.Add(this.btnNew); this.plBaseButtom.Controls.Add(this.btnSave); this.plBaseButtom.Controls.Add(this.btnExit); this.plBaseButtom.Dock = System.Windows.Forms.DockStyle.Bottom; this.plBaseButtom.Location = new System.Drawing.Point(0, 197); this.plBaseButtom.Name = "plBaseButtom"; this.plBaseButtom.Size = new System.Drawing.Size(611, 50); this.plBaseButtom.TabIndex = 0; // // btnNew // this.btnNew.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnNew.Location = new System.Drawing.Point(362, 15); this.btnNew.Name = "btnNew"; this.btnNew.Size = new System.Drawing.Size(75, 23); this.btnNew.TabIndex = 0; this.btnNew.Text = "新增"; this.btnNew.UseVisualStyleBackColor = true; // // btnSave // this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnSave.Enabled = false; this.btnSave.Location = new System.Drawing.Point(443, 15); this.btnSave.Name = "btnSave"; this.btnSave.Size = new System.Drawing.Size(75, 23); this.btnSave.TabIndex = 1; this.btnSave.Text = "保存"; this.btnSave.UseVisualStyleBackColor = true; // // btnExit // this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnExit.Location = new System.Drawing.Point(524, 15); this.btnExit.Name = "btnExit"; this.btnExit.Size = new System.Drawing.Size(75, 23); this.btnExit.TabIndex = 2; this.btnExit.Text = "退出"; this.btnExit.UseVisualStyleBackColor = true; this.btnExit.Visible = false; // // DefaultForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(611, 247); this.Controls.Add(this.plBaseButtom); this.Name = "DefaultForm"; this.Text = "Form"; this.plBaseButtom.ResumeLayout(false); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Panel plBaseButtom; private System.Windows.Forms.Button btnExit; private System.Windows.Forms.Button btnSave; private System.Windows.Forms.Button btnNew; } }View Code
後臺代碼
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace WinForm { public partial class DefaultForm : Form { public DefaultForm() { InitializeComponent(); this.Load += new EventHandler(DefaultForm_Load); } private void DefaultForm_Load(object sender, EventArgs e) { SetButtonToSort(plBaseButtom, 5); } /// <summary> /// 設置Panel容器中的Button自動排序 /// 當Button的Visible=false時會自動隱藏然後重新排序 /// </summary> /// <param name="panel">Panel控制項</param> /// <param name="btnSpace">每個按鈕之間的間距</param> private void SetButtonToSort(Panel panel, int btnSpace) { int length = 0; List<Button> list = new List<Button>(); Control.ControlCollection ctrs = panel.Controls; foreach (Control ctr in ctrs) { if (ctr is Button) { if (ctr.Visible == true) { list.Add((Button)ctr); length += ctr.Width + btnSpace; } } } int pLength = panel.Width; if (length > pLength) return; int startPos = pLength - length - btnSpace; int xPos = startPos; foreach (Control ctr in list) { ctr.Location = new Point(xPos, ctr.Location.Y); xPos += ctr.Width + btnSpace; } } } }
運行後的效果