2009-05-05 50 views
3

私はかなり広範なUserControlをデザインに組み込んで、サイズ変更を可能にしています。 デザインで四角形が必要なので、含まれているドッキングコントロールも四角形になるように、TableLayoutPanelsのすべての列を同じ幅にする必要があります。TableLayoutPanelのすべての列を正確に同じ幅に自動サイズ設定できますか?

残念ながら、TableLayoutPanelの動作は私にこの結果を与えません。
TableLayoutPanelを使用すると、コントロールの同じパーセンテージを使用するように設定されたすべての列で、(7列のセットで)6つの等しい幅の列と7つの列が可変幅になります。
この現象は、7つのサイズのうち6つに対して、7つの列を共有する列ピクセルの数が等しくなく、第7列がこの不等式のオーバーフローであるために発生することを理解しています。

私が望むと思うのは、他の7つの列がオーバーフローするようなものです。「実際の」列の7つすべてが実際の等幅になることができますが、8列目には許可されます幅は0です。
これまでのところ、この動作を可能にする設定が見つかりません。

TableLayoutPanelをどのようにして私が望むことができるのか誰にでも教えてもらえますか、または多くの回避コードを書く必要がありますか?

EDIT:Yacoderの答えを受けて

、私はいくつかの問題を示し、コード、およびTableLayoutPanelの標準機能を使用して、それを解決するために失敗した、素朴な試みを示している別のとDockプロパティ

が追加されました

問題のデモ:

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 

ナイーブソリューション:

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 

私はこのコードを再描画しても邪魔になりません。

答えて

8

たぶん私はかなりそれは私がやったことだ質問を理解し、しかし...しませんでした:

  1. は、フォームに
  2. ドロップTableLayoutPanel新しいフォームを作成し、ドック=
  3. 塗りつぶし設定フォームデザインウィンドウ(VSを使用している場合)で、TableLayoutPanelの小さな矢印をクリックしてタスクを含むメニューを開き、「行と列を編集...」に移動します。
  4. このウィンドウでは、それらのすべてを、全体のサイズの「パーセント」を取って、どこにでも同じ数を入れるように設定します。あなたは任意の数を置くことができます、それが同じであることを確認するだけで、VSは自動的に合計を100%に等しくします。

...少なくとも私がこのようなフォームのサイズを変更すると、すべての列が一緒にサイズ変更されて表示されます。

あなたはVSではないか、その方法とのトラブルを持っている場合、これは私が私のデザイナークラスで自動生成したコードです:

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)); 
+0

残念ながら、これは機能しません。このような設定をしてサイズを変更すると、最後に変更幅が6になる列が変更され、7回目にすべての列がサイズ変更されます。 N列のコレクション内の最終列の幅は、他のすべての列の幅よりも最大(N-1)ピクセル大きい場合があります。リサイズ時にこれを実証するには、CellBorderStyleをSingleに、DockをFillに設定してみてください。 – Frosty840

+1

大丈夫なので、いつも同じ幅が正確に必要ですか?あなたが3列の11ピクセルを持っているように...コントロールの外側に3ピクセル幅と2ピクセルを残したいのですか? –

1

私のように私の環境でこれを設定しようとしていますあなたの投稿を約6〜7回読むと、あなたがしようとしていることを正確に判断できません。私は簡単にそれらがサイズ変更されるのを見ることができるように同じ%幅に設定された列で塗りつぶすためにドッキングされたSWF GroupBoxオブジェクトをドッキングした6列を持っています。私はまた、オーバーフローとして動作するように7px絶対幅に設定された第7列を持っています。これは私が期待するように動作します。

私が判断できないことは、この設定に何が間違っているか、そして/または何をしようとしているかです。あなたは、TableLayoutPanelに必要なものをより良く記述してください。

+0

私は、最初の列が他の列と同じ時間にサイズ変更されないように見えることを発見しました。私の元の投稿の編集の「素朴な解決策」では、ウィンドウを拡大すると、大部分のケースでオーバーフローカラムのサイズが変更され、大きな変更の場合はカラム1-6がサイズ変更され、その後にサイズ変更の場合はカラム0がサイズ変更されますそれ。この問題は、最初の行、奇妙な影響を与えるようには思われません。 – Frosty840

0
public MainForm() 
{ 
    TableLayoutPanel pnlDragAndDrop = new TableLayoutPanel(); 

    // make the panel full width 
    pnlDragAndDrop.Dock = DockStyle.Fill; 

    // be sure to add columns and rows explicitly! 
    pnlDragAndDrop.ColumnCount = 2; 
    pnlDragAndDrop.RowCount = 1; 

    // add a border just for testing 
    pnlDragAndDrop.CellBorderStyle = 
     TableLayoutPanelCellBorderStyle.InsetDouble; 
    pnlDragAndDrop.CellPaint += 
     new TableLayoutCellPaintEventHandler(TblLayoutPanel_CellPaint); 

    // add a column style for each column! 
    for (int i = 0; i < pnlDragAndDrop.ColumnCount * pnlDragAndDrop.RowCount; ++i) 
    { 
     pnlDragAndDrop.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F)); 
    } 

    // add the panel to the form 
    this.Controls.Add(pnlDragAndDrop); 
} 

private void TblLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e) 
{ 
    // Add a border around each cell 
    e.Graphics.DrawLine(
     Pens.Black, 
     e.CellBounds.Location, 
     new Point(e.CellBounds.Right, e.CellBounds.Top) 
     ); 
} 
関連する問題