TableLayoutPanel的大小 [英] TableLayoutPanel sizing

查看:77
本文介绍了TableLayoutPanel的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能没有为我想要的使用正确的控件.我正在用控件填充表格,我希望每一列都自动调整为包含在其中的控件的大小.例如,一列文本框将比一列复选框宽.由于不同的操作系统,不同的DPI,不同的字体等的复杂性,我不想摆弄我是否可以提供帮助.该表可以水平扩展以适合带有滚动条的控件.使用TableLayoutPanel-或某些其他控件怎么可能?

I may not be using the right control for what I want. I'm filling a table with controls and I want each column to automatically size to the controls contained within it. For example, a column of textboxes will be wider than a column of checkboxes. I don't want to fiddle with measuring if I can help it, due to the complexities of different OS, different DPI, different fonts, etc. The table can expand horizontally to fit the controls, with a scrollbar. How is this possible with a TableLayoutPanel - or some other control?

谢谢.

编辑后添加代码:

    private void UpdateLocations()
    {
        tableLayoutPanel1.RowCount = CurrentSchedule.location.Length + 1;
        tableLayoutPanel1.ColumnCount = 7;
        int row = 1;
        int timeWidth = TextRenderer.MeasureText("00:00:00x", tableLayoutPanel1.Font).Width;

        Label lab = new Label();
        lab.Text = "Location";
        tableLayoutPanel1.Controls.Add(lab, 0, 0);

        lab = new Label();
        lab.Text = "Arrive";
        tableLayoutPanel1.Controls.Add(lab, 1, 0);

        lab = new Label();
        lab.Text = "Depart";
        tableLayoutPanel1.Controls.Add(lab, 2, 0);

        lab = new Label();
        lab.Text = "Pass?";
        tableLayoutPanel1.Controls.Add(lab, 3, 0);

        lab = new Label();
        lab.Text = "Path";
        tableLayoutPanel1.Controls.Add(lab, 4, 0);

        lab = new Label();
        lab.Text = "Plat";
        tableLayoutPanel1.Controls.Add(lab, 5, 0);

        lab = new Label();
        lab.Text = "Line";
        tableLayoutPanel1.Controls.Add(lab, 6, 0);

        foreach (location loc in CurrentSchedule.location)
        {
            TextBox tb = new TextBox();
            tb.Text = loc.locationID;
            tableLayoutPanel1.Controls.Add(tb, 0, row);

            tb = new TextBox();
            tb.Text = loc.arrivalTime;
            tb.Width = timeWidth;
            tableLayoutPanel1.Controls.Add(tb, 1, row);

            tb = new TextBox();
            tb.Text = loc.departureTime;
            tb.Width = timeWidth;
            tableLayoutPanel1.Controls.Add(tb, 2, row);

            CheckBox cb = new CheckBox();
            cb.Checked = loc.passingTime;
            tableLayoutPanel1.Controls.Add(cb, 3, row);

            tb = new TextBox();
            tb.Text = loc.pathCode;
            tableLayoutPanel1.Controls.Add(tb, 4, row);

            tb = new TextBox();
            tb.Text = loc.platformCode;
            tableLayoutPanel1.Controls.Add(tb, 5, row);

            tb = new TextBox();
            tb.Text = loc.lineCode;
            tableLayoutPanel1.Controls.Add(tb, 6, row);

            row++;
        }
        /*for (int idx = 0; idx < tableLayoutPanel1.RowCount; idx++)
        {
            tableLayoutPanel1.RowStyles[idx].SizeType = SizeType.AutoSize;
        }
        for (int idx = 0; idx < tableLayoutPanel1.ColumnCount; idx++)
        {
            tableLayoutPanel1.ColumnStyles[idx].SizeType = SizeType.AutoSize;
        }*/
    }

(是的,它需要大量的重构-我只是想让它首先工作)

(Yes it needs heavy refactoring - I'm just trying to get it to work first)

注释掉的位会导致超出范围的异常,即使在逻辑上(对我而言)也不应该.范围似乎仅限于我在设计时设置的值,而不是在运行时设置的值.

The commented out bits cause out of bounds exceptions, even though logically (to me) it shouldn't. The range appears limited to whatever I set at design time, not at runtime.

推荐答案

对不起,但是将列设置为自动调整大小"又有什么问题呢?那就是TableLayoutPanel所做的,调整列的大小以适合其中的控件.展开表格并具有滚动条将需要您将表格的Autosize属性设置为true,然后将TableLayoutPanel置于另一个启用了滚动条的面板中.但是,除非我误解了您的要求,否则列大小应该可以立即使用.

Sorry, but what's wrong with having the columns set to Autosize? That's what TableLayoutPanel does, size columns to the fit the controls within it. Expanding the table and having a scrollbar would require you to set the tables Autosize property to true, then sit the TableLayoutPanel within another panel that has scrollbars enabled. But the column sizing should work out of the box if unless I'm misunderstanding your requirements.

仅需确保,您将转至columns属性并将每列的SizeType设置为AutoSize正确吗?不仅是表本身的AutoSize属性?

Just to make sure, you are going to the columns property and setting each column's SizeType to AutoSize right? Not just the AutoSize property of the table itself?

这是你想要的吗?

-邮政编码:

感谢您的代码.我建议您使用Designer来完成很多工作.至少要设置这些列,将它们设置为自动调整大小,然后添加标题标签.

Thanks for the code. I'd suggest that you use designer to do a lot of this. At least to set up the columns, set them to autosize, and add the heading labels.

您可能还需要检出Datagrid控件并将其绑定到您的位置列表.

You also might want to check out the Datagrid control and bind that to your location list.

要使此方法有效,请执行以下操作:

To get this method working though:

1)列大小相同的原因是因为您使用的标题标签没有自动调整大小.它们全都是x像素宽,这正在拉伸列.这样做:

1) the reason your columns look the same size is because the heading labels you're using aren't autosizing. They're all x pixels wide and that's stretching the columns. Do this:

Label lab = new Label();
lab.AutoSize = true;
lab.Text = "Location";
tableLayoutPanel1.Controls.Add(lab, 0, 0);

您还需要将CheckBox控件和添加为内容的任何其他标签上的AutoSize属性设置为true.

You'll also need to set the AutoSize property to true on the CheckBox control and any other labels you add as content.

2)设置RowCount和ColumnCount不会影响RowStyles或ColumnStyles集合.您有7个Columns,但只有2个ColumnStyles.试试:

2) Setting the RowCount and ColumnCount won't affect the RowStyles or ColumnStyles collection. You've got 7 Columns but only 2 ColumnStyles. Try:

tableLayoutPanel1.ColumnStyles.Clear();
for (int i = 0; i < tableLayoutPanel1.ColumnCount; i++)
{
   tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
}

tableLayoutPanel1.RowStyles.Clear();
for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
{
   tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
}

仅需注意的另一件事是某些控件在行中未对齐(例如,标签显示得太高).要解决此问题,通常会将Margin属性设置为3、6、3、0,以使其与文本框和复选框等对齐.

Only other thing to look out for is that some controls will be misaligned in the rows (labels appear too high for example). To fix that set the Margin property, normally to 3,6,3,0 to align them with textboxes and checkboxes etc.

这篇关于TableLayoutPanel的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆