当前位置:首页 > .NET > C#

C#中Panel器皿里面的Button自动排序

优良自学吧提供C#中Panel器皿里面的Button自动排序,C#中Panel容器里面的Button自动排序在页面中添加Panel容器和Button控件 这是设计器代码 namespace WinForm { partial class Defa

C#中Panel容器里面的Button自动排序

在页面中添加Panel容器和Button控件

C#中Panel器皿里面的Button自动排序

这是设计器代码

C#中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;
            }
        }
    }
}

运行后的效果

C#中Panel器皿里面的Button自动排序

 


(本文来自互联网,不代表搜站(http://www.ylzx8.cn/)的观点和立场)
本站所有内容来自互联网,若本站收录的信息无意侵犯了贵司版权,请给我们来信(ylzx8cn@163.com),我们会及时处理和回复,谢谢