动态创建控件布局与自动大小跟随

本文详细介绍了使用C#在Windows Forms应用程序中动态创建和管理控件的方法,包括文本框、数据网格视图等,展示了如何通过代码设置控件属性和布局,实现灵活的界面设计。

有人认为用写的动态创建控件很高端,其实我更喜欢拖放控件进行各种属性和事件设置很方便,而不是用写的进行各种属性和事件的设置,结合以前写的例子,对各类设置做了归类,比较方便集中写作。


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace 文字提取到数据文件

{

    public partial class 文字提取到数据文件 : Form

    {

        DataGridView 数据列表 = new DataGridView();

        TextBox 录入框 = new TextBox();

        ToolTip 控件说明 = new ToolTip();

        TreeView 分类树 = new TreeView();

        CheckBox 肝 = new CheckBox(), 心 = new CheckBox(), 脾 = new CheckBox(), 肺 = new CheckBox(), 肾 = new CheckBox(), 

            热 = new CheckBox(), 温 = new CheckBox(), 寒 = new CheckBox(), 凉 = new CheckBox(), 平 = new CheckBox();

 

        public 文字提取到数据文件()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            跟随(); 停靠(); 尺寸(); 布局(); 初值(); 注明();

            动态文本框();

            动态表格(new string[] { "名称", "别名", "性味", "归经", "主治" });

        }

        //private void 动态选项框()

        //{

        //    int i = 分类树.Width + 5;

        //    string[] 五脏五味 = { "肝(酸)", "心(苦)", "脾(甘)", "肺(辛)", "肾(咸)" };

        //    foreach (string 名 in 五脏五味)

        //    {

        //        CheckBox 选项框 = new CheckBox();

        //        选项框.Text = 名;

        //        选项框.Name = 名.Remove(1);

        //        选项框.Location = new Point(i, 17);

        //        选项框.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);

        //        选项框.Parent = this;

        //        this.Controls.Add(选项框);

        //        i += 105;

        //    };

        //}

        private void 动态文本框()

        {

            录入框.Text = "";

            录入框.Name = string.Format("文本框{0}", 1);

            录入框.AllowDrop = true;

            录入框.Multiline = true;

            //this.Controls.Add(录入框);

        }

        private void 动态表格(string[] 数据格)

        {

            DataTable 数据表 = new DataTable("数据表");

            foreach (string 格 in 数据格)

                数据表.Columns.Add(格, typeof(string));

 

            数据列表.AllowUserToAddRows = false;

            数据列表.AllowUserToDeleteRows = false;

            数据列表.AllowUserToOrderColumns = false;

            数据列表.AllowUserToResizeColumns = false;

            数据列表.AllowUserToResizeRows = false;

            数据列表.ReadOnly = true;

            数据列表.RowHeadersVisible = false;

            数据列表.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            数据列表.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            数据列表.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;

            数据列表.RowsDefaultCellStyle.WrapMode = (DataGridViewTriState.True);

            数据列表.DefaultCellStyle.SelectionBackColor = Color.Transparent;

            数据列表.DefaultCellStyle.SelectionForeColor = Color.Red;/*.Blue.Gold*/

            数据列表.GridColor = Color.Lime;

            数据列表.DataSource = 数据表;

        }

 

        private void 文字提取到数据文件_Resize(object sender, EventArgs e)

        {

            //MessageBox.Show("最大化!");

            //this.Invalidate();

            //this.Refresh();

            跟随(); 停靠(); 尺寸(); 初值(); 布局(); 注明();

 

            //System.IO.FileStream 打开 = new System.IO.FileStream(Application.StartupPath + @"\新建文本文档.txt", System.IO.FileMode.Open);

            //System.IO.StreamReader 读取 = new System.IO.StreamReader(打开, Encoding.GetEncoding("GB2312"));

            //while (!读取.EndOfStream)

            //    数据表.Rows.Add(读取.ReadLine().Split(' '));

            //打开.Close();

        }

        void 跟随()

        {

            数据列表.Anchor =

                录入框.Anchor =

                肝.Anchor = 心.Anchor = 脾.Anchor = 肺.Anchor = 肾.Anchor =

                热.Anchor = 温.Anchor = 寒.Anchor = 凉.Anchor = 平.Anchor =

                分类树.Anchor = (AnchorStyles.Top | AnchorStyles.Left);

        }

        void 停靠()

        {

            数据列表.Parent =

                录入框.Parent =

                肝.Parent = 心.Parent = 脾.Parent = 肺.Parent = 肾.Parent = 

                热.Parent = 温.Parent = 寒.Parent = 凉.Parent = 平.Parent =

                分类树.Parent = this;

        }

        void 尺寸()

        {

            分类树.Size = new Size(this.Width / 5, this.Height - 42);

            数据列表.Size = new Size(this.Width - 分类树.Width - 24, this.Height / 2);

            录入框.Size = new Size(this.Width - 分类树.Width - 25, this.Height / 2 - 75);

        }

        void 布局()

        {

            分类树.Location = new Point(2, 2);
            数据列表.Location = new Point(分类树.Right + 5, 2);
            录入框.Location = new Point(分类树.Right + 5, 数据列表.Bottom + 5);

            肝.Location = new Point(分类树.Width + 5, this.Height - 65);

            心.Location = new Point(肝.Location.X + 65, this.Height - 65);

            脾.Location = new Point(心.Location.X + 65, this.Height - 65);

            肺.Location = new Point(脾.Location.X + 65, this.Height - 65);

            肾.Location = new Point(肺.Location.X + 65, this.Height - 65);

            热.Location = new Point(肾.Location.X + 65, this.Height - 65);

            温.Location = new Point(热.Location.X + 35, this.Height - 65);

            寒.Location = new Point(温.Location.X + 35, this.Height - 65);

            凉.Location = new Point(寒.Location.X + 35, this.Height - 65);

            平.Location = new Point(凉.Location.X + 35, this.Height - 65);

        }

        void 初值()

        {

            this.BackColor = Color.Red;

            string[] 五脏五味 = { "肝(酸)", "心(苦)", "脾(甘)", "肺(辛)", "肾(咸)", "热", "温", "寒", "凉", "平" };

            int 啊 = 五脏五味.Length;

            Control.ControlCollection 找选项 = this.Controls;

            foreach (Control 选项 in 找选项)

                if (选项 is CheckBox) 选项.Text = 五脏五味[--啊];

        }

        void 注明()

        {

            控件说明.SetToolTip(录入框, "请输入数据文本以便进行分割到数据文件保存。");

            控件说明.SetToolTip(分类树, "显示选择的分类结构。");

        }

 

    }

}

 

 

一.该类的作用: 该类可以帮大家自动布局界面控件,不需要开发人员每个控件的设置属性,只需要调用方法,自动会设置该控件布局,并且控件的宽度随着窗体的变化而变化,该方法调用很简单 二.原理:使用TableLayOutPanle的功能,然后设定里面每个控件的样式 三.使用方法: 1)首先在录入数据的地方用GroupBox或者Panle作为容器(目前里面配置了这2中数据信息用户可以在ParentControlHeader类中进行相应配置) 2)然后在该容器中加入TableLayOutPanle控件,并设定行和列(例如:设定6列,奇数列的宽度都是绝对值:例如100px ,偶数列的宽度都设定为33%) 3)大家可以把相应的控件放入到TableLayOutPanle的相应单元格子中,(奇数列是标题列,偶数列是输入列) 4)在Load事件中这样调用就OK了 TableFormat tf = new TableFormat(tableLayoutPanel1); //此方法可以适用于父级控件是GroupBox或者Panel,您也可一修改 ParentControlHeader类中的配置文件,加入新的值,或者是修改已经设定的值 tf.SetTableFormat(true, PControlType.GroupControl); 这样,大家不需要设定每个TableLayout控件中的子控件的任何属性,TableFormat类帮自动布局界面上的控件,并且随着窗体的变化,控件自动变化的,当然里面有些参数,是可以设定父级控件(GroupPanle/Panle的高度=里面行高(自动计算)+用户配置高度(目前配置了GroupBox和Panle) 详细可见Demo,代码注释写的比较详细,大家可以参考下. 谢谢..^_^.. (鼓励0资源分上传)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值