views:

1660

answers:

2

Hi,
I'm trying to create a fairly extensive UserControl incorporating square controls into the design, and allowing resizing. Because the design calls for squares, I need all the columns in my TableLayoutPanels to be the same width, so that the contained, docked controls are also square.

Unfortunately, the behaviour of the TableLayoutPanel does not give me this result.
Using a TableLayoutPanel with all columns set to use the same percentage of the Control gives (in a set of 7 columns) 6 columns of equal width, and a 7th column which is of variable width.
I understand that this behaviour occurs because for 6 out of every 7 sizes, there are not an equal number of column-pixels to share around the 7 columns, and the 7th column is the overflow for this inequality.

What I guess I want is something like an 8th column that takes the overflow of the other 7 columns, allowing all 7 of the "real" columns to be of real equal widths, but for the 8th column to be allowed to be of 0 width.
So far, I cannot find a setting which allows this behaviour.

Can anyone tell me how to get the TableLayoutPanel to do what I want, or am I going to have to start writing a lot of workaround code?

EDIT:

In response to Yacoder's answer, I have added some code which demonstrates the problem, and another which shows an unsuccessful, naive attempt at solving it using standard features of the TableLayoutPanel and the Dock property

Problem demonstration:

Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
    Me.InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel
    Me.SuspendLayout()
    '
    'TableLayoutPanel1
    '
    Me.TableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.[Single]
    Me.TableLayoutPanel1.ColumnCount = 7
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill
    Me.TableLayoutPanel1.Location = New System.Drawing.Point(0, 0)
    Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
    Me.TableLayoutPanel1.RowCount = 7
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.Size = New System.Drawing.Size(261, 264)
    Me.TableLayoutPanel1.TabIndex = 0
    '
    'Form1
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(261, 264)
    Me.Controls.Add(Me.TableLayoutPanel1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout(False)

End Sub
Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
Private labelList As List(Of Label)
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)
    labelList = New List(Of Label)
    For JJ As Integer = 0 To Me.TableLayoutPanel1.ColumnCount - 1
        For II As Integer = 0 To Me.TableLayoutPanel1.RowCount - 1
            Dim addLabel As New Label
            Me.TableLayoutPanel1.Controls.Add(addLabel, JJ, II)
            addLabel.Dock = DockStyle.Fill
            Me.labelList.Add(addLabel)
        Next
    Next
End Sub
Private Sub TableLayoutPanel1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles TableLayoutPanel1.Resize
    If Me.labelList IsNot Nothing Then
        For Each labelIn As Label In Me.labelList
            labelIn.Text = labelIn.Width.ToString & ", " & labelIn.Height.ToString
        Next
    End If
End Sub
End Class

Naive solution:

Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
    Me.InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel
    Me.SuspendLayout()
    '
    'TableLayoutPanel1
    '
    Me.TableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.[Single]
    Me.TableLayoutPanel1.ColumnCount = 8
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 0.0!))
    Me.TableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill
    Me.TableLayoutPanel1.Location = New System.Drawing.Point(0, 0)
    Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
    Me.TableLayoutPanel1.RowCount = 8
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 0.0!))
    Me.TableLayoutPanel1.Size = New System.Drawing.Size(261, 264)
    Me.TableLayoutPanel1.TabIndex = 0
    '
    'Form1
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(261, 264)
    Me.Controls.Add(Me.TableLayoutPanel1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout(False)

End Sub
Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
Private labelList As List(Of Label)
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)
    labelList = New List(Of Label)
    For JJ As Integer = 0 To Me.TableLayoutPanel1.ColumnCount - 1
        For II As Integer = 0 To Me.TableLayoutPanel1.RowCount - 1
            Dim addLabel As New Label
            Me.TableLayoutPanel1.Controls.Add(addLabel, JJ, II)
            addLabel.Dock = DockStyle.Fill
            Me.labelList.Add(addLabel)
        Next
    Next
End Sub
Private Sub TableLayoutPanel1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles TableLayoutPanel1.Resize
    If Me.labelList IsNot Nothing Then
        For Each labelIn As Label In Me.labelList
            labelIn.Text = labelIn.Width.ToString & ", " & labelIn.Height.ToString
        Next
    End If
End Sub
End Class

My apologies for the abysmal performance of this code on redrawing.

+1  A: 

Maybe I didn't quite understand the question, but... that's what I did:

  1. Create new form
  2. Drop a TableLayoutPanel on the form, and set Dock = Fill
  3. In Form design window (if you are using VS) click on the small arrow on TableLayoutPanel to open a menu with tasks and go to "Edit Rows and Columns..."
  4. In that window add as many columns as you need and set all of them to take "Percent" of the whole size and put the same number everywhere. You can put any number, just ensure it is the same, VS will make the sum equal to 100% automatically.

And... it should work, at least when I resize such form I see all columns resized together...

If you are not in VS, or have troubles with that method, this is the code I have auto-generated in my Designer class:

    this.tableLayoutPanel1.ColumnCount = 7;
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
Yacoder
Unfortunately, this does not work. If you take such a setup and resize it, the last column with change widths every 6 changes, and on the 7th change, all columns will resize. The width of the final column in a collection of N columns can be up to (N-1) pixels greater than the widths of all of the other columns. Try setting the CellBorderStyle to Single and the Dock to Fill in order to demonstrate this when resizing.
Frosty840
ok, so you need EXACTLY the same widths all the time? Like if you have 11 pixels for 3 columns... you want them to be 3px wide and 2 px left outside the control?
Yacoder
+1  A: 

I have tried to set this up in my environment as well as reading your post about 6 or 7 times and I can't determine what it is exactly that you are trying to do. I have 6 columns that have docked SWF GroupBox objects docked to fill with the columns set to the same % width so I can easily see them resize. I also have a 7th column set to 0px absolute width to work as an overflow. This works as I would expect it to.

What I can't determine is what is wrong with this setup and/or what you are trying to get it to do. Can you please describe better what you need the TableLayoutPanel to do?

Chris Porter
I have found that the first column appearts not to resize at the same time as the other columns. In the "naive solution" in the edit to my original post, enlarging the window would cause the overflow column to resize for most cases, columns 1-6 to resize on the big change case, and then column 0 to resize on the resize after that. This problem doesn't seem to affect the first row, oddly.
Frosty840