WinForm——TableLayoutPanel实例

举报
AI浩 发表于 2021/12/22 23:20:38 2021/12/22
【摘要】 实例1、用TableLayoutPanel 制作表格   声明TableLayoutPanel对象    /// <summary>         /// TableLayoutPanel &nbsp...

实例1、用TableLayoutPanel 制作表格

 

声明TableLayoutPanel对象

   /// <summary>

        /// TableLayoutPanel

        /// </summary>

TableLayoutPanel table = new TableLayoutPanel();

添加控件和事件

  private void Form2_Load(object sender, EventArgs e)

        {

            // 默认添加一行数据

            table.Dock = DockStyle.Top;     //顶部填充

            panel1.Controls.Add(table);    

            table.ColumnCount = 5;          //5

            table.Height = table.RowCount * 40; //table的整体高度,每行40

 

            table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width * 0.2f));    //利用百分比计算,0.2f表示占用本行长度的20%

            table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width * 0.2f));

            table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width * 0.2f));

            table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width * 0.2f));

            table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width * 0.2f));

 

            for (int i = 1; i <= 10; i++)

            {

                AddRow("键盘侠"+i.ToString().PadLeft(2,'0'),"蜘蛛侠" + i.ToString().PadLeft(2, '0'), "钢铁侠" + i.ToString().PadLeft(2, '0'), "猪猪侠" + i.ToString().PadLeft(2, '0'), "死猪佩奇" + i.ToString().PadLeft(2, '0'));

            }

        }

 

        private void AddRow(string apple, string orange, string banana, string casaba, string sugarcane)

        {

            try

            {

                // 动态添加一行

                table.RowCount++;

                //设置高度,边框线也算高度,所以将40修改大一点

                table.Height = table.RowCount * 44;

                // 行高

                table.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40));

                // 设置cell样式,增加线条

                table.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetPartial;

 

                int i = table.RowCount - 1;

 

                Label label1 = new Label();

                label1.Text = apple;

                label1.Dock = DockStyle.Fill;

                label1.BackColor = Color.Red;

                label1.Click += Label1_Click;

                label1.Font = new Font("楷体", 13, FontStyle.Regular);

                label1.TextAlign = ContentAlignment.MiddleCenter;

                table.Controls.Add(label1, 0, i);

 

                Label label2= new Label();

                label2.Text = orange;

                label2.Width = 200;

                label2.Height = 40;

                label2.Click += Label1_Click;

                label2.Font = new Font("楷体", 13, FontStyle.Regular);

                label2.TextAlign = ContentAlignment.MiddleCenter;

                table.Controls.Add(label2, 1, i);

 

                Label label3 = new Label();

                label3.Text = banana;

                label3.Width = 200;

                label3.Height = 40;

                label3.Click += Label1_Click;

                label3.Font = new Font("楷体", 13, FontStyle.Regular);

                label3.TextAlign = ContentAlignment.MiddleCenter;

                table.Controls.Add(label3, 2, i);

                Label label4 = new Label();

                label4.Text = casaba;

                label4.Width = 200;

                label4.Height = 40;

                label4.Click += Label1_Click;

                label4.Font = new Font("楷体", 13, FontStyle.Regular);

                label4.TextAlign = ContentAlignment.MiddleCenter;

                table.Controls.Add(label4, 3, i);

                Label label5 = new Label();

                label5.Text = sugarcane;

                label5.Width = 200;

                label5.Height = 40;

                label5.Click += Label1_Click;

                label5.Font = new Font("楷体", 13, FontStyle.Regular);

                label5.TextAlign = ContentAlignment.MiddleCenter;

                table.Controls.Add(label5, 4, i);

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message.PadRight(30, ' '), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }

 

        private void Label1_Click(object sender, EventArgs e)

        {

            Label label = (Label)sender;

            MessageBox.Show(label.Text);

        }

 

实例2、实现表格的跨列

在TableLayoutPanel中加入panel对象,设置panel对象的ColumnSpan属性为2,将Marign设置为0,Dock设置为Fill。

 

 

文章来源: wanghao.blog.csdn.net,作者:AI浩,版权归原作者所有,如需转载,请联系作者。

原文链接:wanghao.blog.csdn.net/article/details/107835709

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。