DOBON.NET

TableLayoutPanelの行や列を挿入する

注意:TableLayoutPanelコントロールは.NET Framework 2.0以降でのみ使用できます。

Visual Studioのフォームデザイナを使ってTableLayoutPanelコントロールに行や列を挿入する方法は、「TableLayoutPanelコントロールを使って、コントロールを表形式で整列させる」で説明しました。フォームデザイナでは簡単ですが、これを実行時に行ううまい方法はありません。RowStyles.Insertメソッドでうまくいかないことは、「TableLayoutPanelの行や列の幅を変更する」を読んでいただければ、明白です。結局は、行や列を拡張して、TableLayoutPanelに配置されたコントロールを一つずつ順番に移動されるという地味なやり方にしかなさそうです。

しかし単純にそのようなコードを書いたとしてもうまくいくとは限りません。なぜならば、実行時にTableLayoutPanelに配置されたコントロールはどこに配置されるか予測が付きにくく、新しく挿入された行に入ってきてしまう可能性があるからです(それでいいと言うのであれば、かまわないでしょうが)。

よってここでは、「Controls.Addで指定した位置に挿入できない問題」で紹介したように、TableLayoutPanelに配置されたコントロールの位置を実際に現在ある位置に設定してから、列を挿入する処理を行うことにします。

以下の例では、insertRowの位置に行を挿入しています。「TableLayoutPanelコントロールを使って、コントロールを表形式で整列させる」で紹介しているサンプル「TableLayoutPanel1.exe」からの抜粋です。

VB.NET
コードを隠すコードを選択
TableLayoutPanel1.SuspendLayout()

Dim c As Control
For Each c In TableLayoutPanel1.Controls
    Dim pos As TableLayoutPanelCellPosition = _
        TableLayoutPanel1.GetPositionFromControl(c)
    TableLayoutPanel1.SetCellPosition(c, pos)
    If TableLayoutPanel1.RowCount <= pos.Row Then
        TableLayoutPanel1.RowCount = pos.Row + 1
    End If
    If TableLayoutPanel1.ColumnCount <= pos.Column Then
        TableLayoutPanel1.ColumnCount = pos.Column + 1
    End If
Next c

'列を増やす
TableLayoutPanel1.RowCount += 1

'コントロールを移動
Dim y As Integer
For y = TableLayoutPanel1.RowCount - 1 To insertRow Step -1
    Dim x As Integer
    For x = 0 To TableLayoutPanel1.ColumnCount - 1
        c = TableLayoutPanel1.GetControlFromPosition(x, y)
        If Not (c Is Nothing) Then
            TableLayoutPanel1.SetCellPosition( _
                c, New TableLayoutPanelCellPosition(x, y + 1))
        End If
    Next x
Next y

'スタイルを挿入
If TableLayoutPanel1.RowStyles.Count > insertRow Then
    TableLayoutPanel1.RowStyles.Insert( _
        insertRow, New RowStyle(SizeType.AutoSize))
End If

TableLayoutPanel1.ResumeLayout()
C#
コードを隠すコードを選択
tableLayoutPanel1.SuspendLayout();

foreach (Control c in tableLayoutPanel1.Controls)
{
    TableLayoutPanelCellPosition pos =
        tableLayoutPanel1.GetPositionFromControl(c);
    tableLayoutPanel1.SetCellPosition(c, pos);
    if (tableLayoutPanel1.RowCount <= pos.Row)
        tableLayoutPanel1.RowCount = pos.Row + 1;
    if (tableLayoutPanel1.ColumnCount <= pos.Column)
        tableLayoutPanel1.ColumnCount = pos.Column + 1;
}

//列を増やす
tableLayoutPanel1.RowCount++;

//コントロールを移動
for (int y = tableLayoutPanel1.RowCount - 1; y >= insertRow; y--)
{
    for (int x = 0; x < tableLayoutPanel1.ColumnCount; x++)
    {
        Control c = tableLayoutPanel1.GetControlFromPosition(x, y);
        if (c != null)
        {
            tableLayoutPanel1.SetCellPosition(
                c, new TableLayoutPanelCellPosition(x, y + 1));
        }
    }
}

//スタイルを挿入
if (tableLayoutPanel1.RowStyles.Count > insertRow)
{
    tableLayoutPanel1.RowStyles.Insert(
        insertRow, new RowStyle(SizeType.AutoSize));
}

tableLayoutPanel1.ResumeLayout();

注意:この記事では、基本的な事柄の説明が省略されているかもしれません。初心者の方は、特に以下の点にご注意ください。

  • .NET Tipsをご利用いただく際は、注意事項をお守りください。
共有する

この記事への評価

この記事へのコメント

この記事に関するコメントを投稿するには、下のボタンをクリックしてください。投稿フォームへ移動します。通常のご質問、ご意見等は掲示板へご投稿ください。