تعین موقعیت و سایز کنترل در زمان اجرا
ابتدا یکسری از فضای نامهای مورد نیاز رو به برنامه اضافه می کنیم .
using System.Collections.Generic;
using System.Drawing;
بعد این متد رو در یک کلاس بنویسین :
public static void GetControlLocations(int ParentWidth,
int ParentHeight, int TotalRows, int TotalColumns,
int Gap, out Size ControlSize, out List<Point> ControlLocations)
{
int column = 1;
int row = 1;
int width = (int)((ParentWidth - (TotalRows + 1) * Gap) / TotalRows);
int height = (int)((ParentHeight - (TotalColumns + 1) * Gap) / TotalColumns);
int startX = Gap;
int startY = Gap;
ControlSize = new Size(width, height);
ControlLocations = new List<Point>();
for (int i = 0; i < TotalRows * TotalColumns; i++)
{
if (column > TotalRows)
{
column = 1;
row++;
}
ControlLocations.Add(new Point((width * (column - 1)) +
(Gap * column), (height * (row - 1)) + (Gap * row)));
column++;
}
}
خوب ! حالا از کد در برنامه استفاده می کنیم :
Size controlSize = new Size();
List<Point> controlLocations = new List<Point>();
int requiredControlsCount = 9; //Set this with the required count
if (requiredControlsCount == 1)
GetControlLocations(this.Size.Width, this.Size.Height, 1,
1, 5, out controlSize, out controlLocations);
else if (requiredControlsCount > 1 && requiredControlsCount <= 4)
GetControlLocations(this.Size.Width, this.Size.Height, 2,
2, 5, out controlSize, out controlLocations);
else if (requiredControlsCount > 4 &&requiredControlsCount <= 6)
GetControlLocations(this.Size.Width, this.Size.Height, 3,
2, 5, out controlSize, out controlLocations);
else if (requiredControlsCount > 6 && requiredControlsCount <= 9)
GetControlLocations(this.Size.Width, this.Size.Height, 3,
3, 5, out controlSize, out controlLocations);
else if (requiredControlsCount > 9 && requiredControlsCount <= 12)
GetControlLocations(this.Size.Width, this.Size.Height, 4,
3, 5, out controlSize, out controlLocations);
else if (requiredControlsCount > 12 && requiredControlsCount <= 16)
GetControlLocations(this.Size.Width, this.Size.Height, 4,
4, 5, out controlSize, out controlLocations);
فقط در متغیر (requiredControlsCount) باید شما تعداد کنترل ها یی رو که می خواهید رو به این متغیر بدید این رو هم اضافه کنم که این کد قادر هستش بر روی تمام Container ها عمل کند.
امیدوارم این کد برای شما مفید بوده باشه .
موفق باشید .