C# 使用FlowLayoutPanel动态显示

FlowLayoutPanel可以根据Excel或xml等文件配置显示内容,自动绘画显示。解决新需求变更参数还需要更改界面的的问题。

如图中子窗体(右边部分,右边显示内容根据左边点击后切换):

显示内容可以在配置表中设置,动态添加显示,事件统一处理(对应不同事件参考:C# 自定义按钮及其事件处理_花开莫与流年错_的博客-CSDN博客_c# 自定义按钮

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using study.NewFolder1.Right.Basic;

namespace study.NewFolder1.Right
{
    public partial class Controls : UserControl
    {
        List<Control> controls = new List<Control>();

        Button button1 = new Button();

        public Controls()
        {
            InitializeComponent();
            Init();
        }
        void Init()
        {
            this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.Dock = DockStyle.Fill;
            label1.Text = this.Width + ", " + this.Height;
            this.SizeChanged += new EventHandler(ThisSizeChanged);

            controls.Add(TitlePanel);

            // 右边动态显示内容,可以是读取本地配置表来显示
            List<string> list = new List<string>();
            for (int i = 0; i < 2; i++)
                list.Add(Convert.ToString(i + 1));
            for (int i = 0; i < list.Count; i++)
            {
                // 添加显示内容,并添加事件处理。事件统一用一个函数处理,通过显示内容区分点了哪一个
                Title title = new Title(list[i]);
                title.Changed += new MyEventHandler(Title_Changed);
                // 添加到controls最终显示出来
                controls.Add(title);
            }

            // 添加很多时挂起会更快(不触发界面更改重新绘图事件)
            this.flowLayoutPanel1.SuspendLayout();
            for(int i = 0; i < controls.Count; i++)
            {
                this.flowLayoutPanel1.Controls.Add(controls[i]);
            }
            this.flowLayoutPanel1.ResumeLayout();
        }

        private void Add_Click(object sender, EventArgs e)
        {
            // 添加很多时挂起会更快(不触发界面更改重新绘图事件)
            this.flowLayoutPanel1.SuspendLayout();

            Title title = new Title("laoshijidaidaiwo");
            title.Width = this.Width - 25;
            title.Changed += new MyEventHandler(Title_Changed);
            controls.Add(title);
            this.flowLayoutPanel1.Controls.Add(title);

            this.flowLayoutPanel1.ResumeLayout();
        }
        void Title_Changed(object obj, string str)
        {
            int i = 0;
        }
        // 自适应大小
        void ThisSizeChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < controls.Count; i++)
            {
                controls[i].Width = this.Width - 25;
            }
            label1.Text = this.Width + ", " + this.Height;
        }


        #region 组件设计器生成的代码
        /// <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);
        }

        /// <summary> 
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
            this.TitlePanel = new System.Windows.Forms.Panel();
            this.TitlePicture = new System.Windows.Forms.PictureBox();
            this.Title = new System.Windows.Forms.Label();
            this.Add = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.flowLayoutPanel1.SuspendLayout();
            this.TitlePanel.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.TitlePicture)).BeginInit();
            this.SuspendLayout();
            // 
            // flowLayoutPanel1
            // 
            this.flowLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.flowLayoutPanel1.Controls.Add(this.TitlePanel);
            this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.flowLayoutPanel1.Name = "flowLayoutPanel1";
            this.flowLayoutPanel1.Size = new System.Drawing.Size(954, 435);
            this.flowLayoutPanel1.TabIndex = 0;
            // 
            // TitlePanel
            // 
            this.TitlePanel.AutoScroll = true;
            this.TitlePanel.Controls.Add(this.TitlePicture);
            this.TitlePanel.Controls.Add(this.Title);
            this.TitlePanel.Font = new System.Drawing.Font("宋体", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.TitlePanel.Location = new System.Drawing.Point(3, 3);
            this.TitlePanel.Name = "TitlePanel";
            this.TitlePanel.Size = new System.Drawing.Size(636, 48);
            this.TitlePanel.TabIndex = 2;
            // 
            // TitlePicture
            // 
            this.TitlePicture.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.TitlePicture.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.TitlePicture.Location = new System.Drawing.Point(3, 34);
            this.TitlePicture.Name = "TitlePicture";
            this.TitlePicture.Size = new System.Drawing.Size(600, 2);
            this.TitlePicture.TabIndex = 1;
            this.TitlePicture.TabStop = false;
            // 
            // Title
            // 
            this.Title.AutoSize = true;
            this.Title.Location = new System.Drawing.Point(12, 12);
            this.Title.Name = "Title";
            this.Title.Size = new System.Drawing.Size(47, 19);
            this.Title.TabIndex = 0;
            this.Title.Text = "主页";
            // 
            // Add
            // 
            this.Add.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.Add.Location = new System.Drawing.Point(52, 441);
            this.Add.Name = "Add";
            this.Add.Size = new System.Drawing.Size(82, 32);
            this.Add.TabIndex = 1;
            this.Add.Text = "添加";
            this.Add.UseVisualStyleBackColor = true;
            this.Add.Click += new System.EventHandler(this.Add_Click);
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(177, 458);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(55, 15);
            this.label1.TabIndex = 2;
            this.label1.Text = "label1";
            // 
            // Controls
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(120F, 120F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            this.AutoScroll = true;
            this.AutoSize = true;
            this.Controls.Add(this.label1);
            this.Controls.Add(this.Add);
            this.Controls.Add(this.flowLayoutPanel1);
            this.Name = "Controls";
            this.Size = new System.Drawing.Size(954, 489);
            this.flowLayoutPanel1.ResumeLayout(false);
            this.TitlePanel.ResumeLayout(false);
            this.TitlePanel.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.TitlePicture)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
        private System.Windows.Forms.Button Add;
        private System.Windows.Forms.Panel TitlePanel;
        private System.Windows.Forms.PictureBox TitlePicture;
        private System.Windows.Forms.Label Title;
        private System.Windows.Forms.Label label1;
        private Basic.Title title2;
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值