表的列宽不正确

问题描述 投票:1回答:1

因为我是新手,所以我想为表的列宽设置值时遇到问题(使用代码C#)>

我想像图片一样展示

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9URlpzbi5wbmcifQ==” alt =“在此处输入图像描述”>“ >>

但是当我编码时:

    private void Form1_Load(object sender, EventArgs e)
    {
       // TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();

        tableLayoutPanel.ColumnCount = 3;
        tableLayoutPanel.RowCount = 1;

        tableLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
        tableLayoutPanel.Dock = DockStyle.Top;
        tableLayoutPanel.Height = 100;

        tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 10F));
        tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 60F));
        tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));

        #region Create Label1, label2, label3         
        Label label1 = new Label();
        label1.Text = "Width 10%";
        //label1.Dock = DockStyle.Fill;  

        Label label2 = new Label();
        label2.Text = "Width 60%";
        //label1.Dock = DockStyle.Fill;

        Label label3 = new Label();
        label3.Text = "Width 30%";
        //label1.Dock = DockStyle.Fill;
        #endregion 

        tableLayoutPanel.Controls.Add(label1, 0, 0);
        tableLayoutPanel.Controls.Add(label2,1,0);
        tableLayoutPanel.Controls.Add(label3, 2, 0);
    }

列宽不正确,如下所示“在此处输入图像描述”

我不知道为什么。请告诉我以及如何解决。阅读的感谢声

因为我是新手,所以当我想为表的列宽设置值(使用代码C#)时我遇到一个问题,我想像图片一样显示,但是当我编写代码时:private void Form1_Load(object ...

c# winforms windows-forms-designer column-width
1个回答
0
投票

您可能已经在集合中存在现有的ColumnStyles。设置ColumnCount不会重置该集合。只需将其清除,然后再添加新的:

tableLayoutPanel.ColumnStyles.Clear();
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 10F));
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 60F));
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
© www.soinside.com 2019 - 2024. All rights reserved.