为什么将我的TableLayoutPanel除以百分比时不使用其所有大小? [英] Why doesn't my TableLayoutPanel use all of its size when divided by percentages?

查看:304
本文介绍了为什么将我的TableLayoutPanel除以百分比时不使用其所有大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向TableLayoutPanel动态添加控件。

I am dynamically adding controls to a TableLayoutPanel.

它具有12行32列。

我已经划分了列的宽度,以使带有标签的列
的宽度是带有TextBoxes的列的两倍,并且百分比的总和为100:

I've divided the widths of the columns so that those with labels are twice as wide as those with TextBoxes, and the percentages add up to 100:

labels = 5%
textBoxes = 2.5%

由于每行有24个文本框,每行有8个标签,因此等于100%(40%标签,60%textBoxes)。

As there are 24 textboxes per row and 8 labels per row, this equals 100% (40% labels, 60% textBoxes).

但是,最后一列是一个空格键,如此处所示:

HOWEVER, the last column is a spacehog, as can be seen here:

http://warbler.posterous.com/the-32-column-is-wider-than-the-rest

我需要标签空间来显示它们的整个自我:它们实际上是五个字符宽( 00:00 ... 23:45),而不是四个字符,如图所示。

And I need that space for the labels to show their entire selves: they are really five characters wide ("00:00" ... "23:45") not four, as displayed.

基于经验证据(先前的反复试验),我可以减小e TLP使最后一栏变瘦,但这并不能解决我的label-cells-to-skinny问题。

Based on empirical evidence (previous trial-and-error) I could reduce the width of the TLP to skinny up that last column, but that doesn't solve my label-cells-too-skinny problem.

这就是我对控件进行拍打的方式TLP(我已经在设计时通过Columns集合的属性页设置了列宽百分比)

This is how I'm slapping the controls on the TLP (I've set the column width percentages at design-time, via the properties page for Columns collection)

private void AddPlatypusScheduleControlsToTableLayoutPanel()
{
    try
    {
        this.SuspendLayout();
        tableLayoutPanelPlatypusSchedule.ColumnCount = PLATYPUS_SCHEDULE_COL_COUNT;
        int ColNum = 0;
        int RowNum = 0;
        int LoopCounter = 1;
        var dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);

        while (LoopCounter <= QUARTER_HOUR_COUNT)
        {
            string lblName = string.Format("labelPS{0}", LoopCounter);
            var lbl = new Label
                {
                    Name = lblName,
                    Dock = DockStyle.Fill,
                    Margin = new Padding(),
                    Font = new Font("Microsoft Sans Serif", 7),
                    TextAlign = ContentAlignment.MiddleCenter,
                    Text = dt.ToString("HH:mm")
                };
            tableLayoutPanelPlatypusSchedule.Controls.Add(lbl, ColNum, RowNum);

            string txtbxName1 = string.Format("textBoxLSA{0}", LoopCounter);
            var txtbxa = new TextBox { Name = txtbxName1, Dock = DockStyle.Fill, Margin = new Padding() };
            string txtbxName2 = string.Format("textBoxLSB{0}", LoopCounter);
            var txtbxb = new TextBox { Name = txtbxName2, Dock = DockStyle.Fill, Margin = new Padding() };
            string txtbxName3 = string.Format("textBoxLSC{0}", LoopCounter);
            var txtbxc = new TextBox { Name = txtbxName3, Dock = DockStyle.Fill, Margin = new Padding() };
            tableLayoutPanelPlatypusSchedule.Controls.Add(txtbxa, ColNum + 1, RowNum);
            tableLayoutPanelPlatypusSchedule.Controls.Add(txtbxb, ColNum + 2, RowNum);
            tableLayoutPanelPlatypusSchedule.Controls.Add(txtbxc, ColNum + 3, RowNum);

            dt = dt.AddMinutes(15);
            RowNum++;
            LoopCounter++;
            if (RowNum == 12)
            {
                ColNum = ColNum + 4;
                RowNum = 0;
            }
        }
    }
    finally {
        this.ResumeLayout();
    }
}



UPDATE



我认为问题可能是TLP的宽度不能被32整除(它是619)。所以我将其扩展为640,但仍然存在相同的问题。

UPDATE

I thought maybe the problem was the width of the TLP was not exactly divisible by 32 (it was 619); so I widened it to 640, yet the same problem remains.

要使其正常工作,我必须对列使用绝对大小,而不要使用百分比*,然后将标签保持列的宽度增加到35(TLP宽度为640时,textBox保持列只剩下15)。

To get it to work, I had to use absolute sizes for the columns rather than percentages*, and increase the width of the label-holding columns to 35 (which leaves only 15 for the textBox-holding columns at a TLP width of 640).


  • WPF有时可能是熊(更像是Kodiak,而不是Winnie),但是对于这种情况,我确实很满意,因为我担心绝对尺寸全部以不同的分辨率/屏幕尺寸等到达达拉斯。

推荐答案

是由于浮点数学和整数宽度。

So maybe this is due to floating point math and integral widths.

控件的 ClientSize.Width 是一个整数。无论您是否指定浮点值,表布局最终都需要将其转换为整数。

The ClientSize.Width of a control is an integer. The table layout, regardless if you specify a floating point value, will ultimately need to convert that to something integral.

我整理了一个与您相似的示例。在运行示例时,我获取了控件的 ClientWidth 值,该值是我的窗口大小的284。

I threw together an example similar to yours. When I ran the example, I grabbed the control's ClientWidth value, which was 284 for my window's size.

单元格的边界会有些宽,我猜我说的是1。所以,让我们假设284减去33的边界,所以剩下的251应该分成32列。

The cell borders are going to be some width and as a guess I said they were 1. So, let's assume 284 minus the 33 for the borders, so that is 251 left over that needs to be split up into 32 columns.

对于那些窄列,值为0.025:

For those narrow columns, the value was 0.025:

0.025 * 251 = 6.275, truncated to 6

因此每个窄列都有一个额外的0.275。

So there's an extra 0.275 for each narrow column.

对于那些较宽的列,值是0.05:

For those wider columns, the value was 0.05:

0.05 * 251 = 12.55, truncated to 12

因此,每个宽列都有额外的0.55。

So there's an extra 0.55 for each wide column.

24 * 6 =宽度为24个窄列

24 * 6 = 144 width for the 24 narrow columns

8 * 12 = 96宽度为8个宽列

8 * 12 = 96 width for the 8 wide columns

总共240个。

11剩余了,所以多余的去了最后一列,最后变成了人们会认为这将是(6)+多余的(11),由于浮点数学运算,最终它的宽度是预期的3倍。

11 left over, so the extra goes to the last column and it ends up being what one would think it would be (6) + the extra (11) and it ends up being, say, 3x as wide as you would expect, due to floating point math.

无论如何,这就是我对它为何看起来如此的猜测。

Anyhow, that's my guess on why it looks that way.

这篇关于为什么将我的TableLayoutPanel除以百分比时不使用其所有大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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