分段渲染动态TableLayoutPanel WF

问题描述

我创建了一个类来设计动态 TableLayoutPanel,但在渲染过程中表格表现不佳。这是课程:

using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace office_software.Table {
    class CalibrationTable : TableLayoutPanel {
        
        private readonly string[] header;
        private readonly List<Row> data;

        public CalibrationTable(TableLayoutPanel parent,string[] header,List<Row> data) {
            this.header = header;
            this.data = data;

            parent.SuspendLayout();

            TableLayoutPanel child = TableConstruction();
            if (parent.GetControlFromPosition(0,1) != null) parent.Controls.Remove(parent.GetControlFromPosition(0,1));
            parent.Controls.Add(child,1);

            parent.ResumeLayout(true);
        }

        private void CreateHeader(TableLayoutPanel parent) {
            for (int i = 0; i < header.Length; i++) parent.Controls.Add(new TableLable(FontStyle.Bold,header[i],9.75F),i,0);
        }

        private void CreateBody(TableLayoutPanel parent) {
            int j = 0;
            foreach (Row row in data) {
                for (int i = 0; i < row.values.Count; i++) {
                    parent.Controls.Add(new TableLable(FontStyle.Regular,row.values[i],j + 1);
                }
                j++;
            }
        }

        private TableLayoutPanel TableConstruction() {

            TableLayoutPanel table = new TableLayoutPanel {
                AutoSize = true,AutoScroll = true,};

            table.SuspendLayout();

            if (data?.Count > 0) {
                table.RowCount = data.Count + 1;
                table.ColumnCount = header.Length;
                table.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
                table.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;

                for (int i = 0; i < table.ColumnCount; i++) table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent,(100 / table.ColumnCount)));
                for (int i = 0; i < table.RowCount; i++) table.RowStyles.Add(new RowStyle(SizeType.Absolute,40F));
                
                CreateHeader(table);
                CreateBody(table);
            }
            else {
                table.RowCount = 1;
                table.ColumnCount = 1;
                table.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
                table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent,50F));
                table.RowStyles.Add(new RowStyle(SizeType.Absolute,50F));
                table.Controls.Add(new TableLable(FontStyle.Bold,"NESSUN ELEMENTO TROVATO",15F),0);
            }

            table.ResumeLayout();

            return table;
        }
    }
}

这是表格渲染期间发生的情况。这种情况会持续几秒钟。 “加载”后,表格显示为它应有的样子。

enter image description here

更新

这是 TableLable 类:

using System.Drawing;
using System.Windows.Forms;

namespace office_software.Table {
    class TableLabel : Label {
        public TableLabel(FontStyle style,string text,float size) {
            Text = text;
            AutoSize = true;
            Anchor = AnchorStyles.None;
            TextAlign = ContentAlignment.MiddleCenter;
            Font = new Font("Microsoft Sans Serif",size,style,GraphicsUnit.Point,0);
        }
    }

为了完整起见,这是表单组件层次结构。我将动态表插入 condainerPanel 中的位置 (col = 0,row = 1)

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)