表格布局面板公司#

时间:2014-08-22 06:29:34

标签: vb.net tablelayoutpanel

我必须使用一些具有自动调整大小的行和固定列大小的控件来创建动态表布局面板。 我的问题是我想显示整个复选框文本。 任何帮助 我的代码是

        Dim textBox2 As New CheckBox()
        textBox2.Text = "You forgot to add the ColumnStyles. Do this on a sample form first with the designer. Click the Show All Files icon in the Solution Explorer window. Open the node next to the form and double-click the Designer.vb file. "
        textBox2.AutoSize = True
        textBox2.Dock = DockStyle.Top
        ''  textBox2.Size = New Point(200, 90)
        Dim lbl1 As New Label()
        lbl1.Location = New Point(10, 10)
        lbl1.Text = "Yoer.vb"
        lbl1.AutoSize = True
        lbl1.Location = New Point(120, 50)
        lbl1.Dock = DockStyle.Top
        ''    dynamicTableLayoutPanel.Padding = New Padding(2, 17, 4, 5)
        dynamicTableLayoutPanel.Controls.Add(lbl1, 0, 0)
        dynamicTableLayoutPanel.Controls.Add(textBox2, 1, 0)
        Me.dynamicTableLayoutPanel.SetColumnSpan(textBox2, 5)

2 个答案:

答案 0 :(得分:0)

如果您的意思是希望表格的大小适合其中的控件,那么:

dynamicTableLayoutPanel.AutoSize = True

答案 1 :(得分:0)

我知道这已经过时了,但我偶然发现了它并且发现我会把我的2美分扔进去以防其他人出现。

注意:我正在使用Visual Studio 2015和.NET 4.6。功能可能因版本而异。

问题是真正长的文本不是自动换行以适应表格或表格。相反,它设置为Dock = DockStyle.Top。这将导致它生成一条连续并被剪裁的单行,类似于单行文本框。

如果您希望自动换行,则需要使用Dock = DockStyle.Fill。现在,如果您的行或表不足以显示文本,则无法完全解决问题。由于所有行都设置为AutoSize,因此它只会将最小值设置为垂直适合控件。它不关心文本是否被剪掉。使用示例代码对6列10行表的最终结果如下:

Initial Output

由于没有自动换行属性,您需要手动调整它。现在,要执行此操作,您需要将行更改为Absolute而不是AutoSize。要弄清楚它有多大,你几乎可以依靠PreferredSize。这显示比现有常规Width宽得多Width。从那里,我们可以确定包裹它所需的线数。

这是我的代码最终看起来像:

  Dim h As Single = 0

  Dim chk As New CheckBox()
  chk.Text = "You forgot to add the ColumnStyles. Do this on a sample form first with the designer. Click the Show All Files icon in the Solution Explorer window. Open the node next to the form and double-click the Designer.vb file. "
  chk.AutoSize = True
  chk.Dock = DockStyle.Fill

  Dim lbl1 As New Label()
  lbl1.Text = "Yoer.vb"
  lbl1.AutoSize = True
  lbl1.Dock = DockStyle.Top

  dynamicTableLayoutPanel.Controls.Add(lbl1, 0, 0)
  dynamicTableLayoutPanel.Controls.Add(chk, 1, 0)
  dynamicTableLayoutPanel.SetColumnSpan(chk, 5)

  ' Find the preferred width, divide by actual, and round up.
  ' This will be how many lines it should take.
  h = Math.Ceiling(chk.PreferredSize.Width / chk.Width)
  ' Multiply the number of lines by the current height.
  h = (h * chk.PreferredSize.Height)
  ' Absolute size the parent row to match this new height.
  dynamicTableLayoutPanel.RowStyles.Item(0) = New RowStyle(SizeType.Absolute, h)

更改包括声明高度变量,重命名CheckBox变量,将其Dock设置为Fill,从lbl1中删除Location,以及添加大小计算。输出:

Changed Output

这并不完美,因为高度包含复选框本身,并且复选框占用填充,因此可以计算太多或太少的高度。还有其他计算可能需要考虑。但是,这是一个起点。

相关问题