add TableLayoutPanel extension methods for add/remove rows
This commit is contained in:
66
Pilz.UI.WinForms/Extensions/TableLayoutPanelExtensions.cs
Normal file
66
Pilz.UI.WinForms/Extensions/TableLayoutPanelExtensions.cs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
<Nullable>annotations</Nullable>
|
<Nullable>annotations</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Version>2.6.1</Version>
|
<Version>2.6.2</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
|
|||||||
Reference in New Issue
Block a user