67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
namespace Pilz.UI.WinForms.Extensions;
|
|
|
|
public static class TableLayoutPanelExtensions
|
|
{
|
|
public static void InsertRows(this TableLayoutPanel panel, int row, int amount)
|
|
{
|
|
if (row < 0 || row > panel.RowCount)
|
|
throw new ArgumentOutOfRangeException(nameof(row));
|
|
|
|
if (amount <= 0)
|
|
return;
|
|
|
|
panel.SuspendLayout();
|
|
panel.RowCount += amount;
|
|
|
|
for (int i = 0; i < amount; i++)
|
|
panel.RowStyles.Insert(row, new RowStyle(SizeType.AutoSize));
|
|
|
|
foreach (var control in panel.Controls.Cast<Control>().OrderByDescending(panel.GetRow))
|
|
{
|
|
var currentRow = panel.GetRow(control);
|
|
|
|
if (currentRow >= row)
|
|
panel.SetRow(control, currentRow + amount);
|
|
}
|
|
|
|
panel.ResumeLayout();
|
|
}
|
|
|
|
public static void RemoveRows(this TableLayoutPanel panel, int row, int amount)
|
|
{
|
|
if (row < 0 || row >= panel.RowCount)
|
|
throw new ArgumentOutOfRangeException(nameof(row));
|
|
|
|
if (amount <= 0 || row + amount > panel.RowCount)
|
|
throw new ArgumentOutOfRangeException(nameof(amount));
|
|
|
|
panel.SuspendLayout();
|
|
|
|
foreach (var control in panel.Controls.Cast<Control>().ToList())
|
|
{
|
|
int r = panel.GetRow(control);
|
|
if (r >= row && r < row + amount)
|
|
{
|
|
panel.Controls.Remove(control);
|
|
control.Dispose();
|
|
}
|
|
}
|
|
|
|
foreach (var control in panel.Controls.Cast<Control>())
|
|
{
|
|
int r = panel.GetRow(control);
|
|
if (r >= row + amount)
|
|
panel.SetRow(control, r - amount);
|
|
}
|
|
|
|
for (int i = 0; i < amount; i++)
|
|
{
|
|
if (panel.RowStyles.Count > row)
|
|
panel.RowStyles.RemoveAt(row);
|
|
}
|
|
|
|
panel.RowCount -= amount;
|
|
panel.ResumeLayout();
|
|
}
|
|
}
|