다음은 그리드를 생성하는 방법이며,이 "그리드"의 "셀"에 임의의 컨트롤을 배치 할 수 있습니다. 이 코드를 실행하려면이 코드를 단추 처리기에 붙여 넣고 필요한 모든 작업을 수행하십시오. 이것이 정확한 해결책인지는 모르겠지만 문제가 발생할 수 있습니다. 다른 주에서 화면 모의를 할 수 있다면 도움이 될 것입니다. 그래서 우리는 실제로 원하는 것을 이해합니다.
일부 단추 처리기에
전화 방법 DoAGrid
:
DoAGrid(bool.Parse(_txtTest.Text)); // type true or false in txt
Methos는
private void DoAGrid(bool isTest)
{
const int size = 30; // I give 2 for controll padding
const int padding = 20; // x and y starting padding
Point[,] grid = new Point[10,10]; // x and y of each control
List<Control> btns = null;
if (isTest) btns = new List<Control>(100);
for (int x = 1; x < 11; x++)
{
for (int y = 1; y < 11; y++)
{
grid[x - 1, y - 1] = new Point(padding + x*size - 30 - 1, padding + y*size - 30 - 1); // 30 - 1 --> size + 2
if (isTest)
{ // this test will add all avail buttons so you can see how grid is formed
Button b = new Button();
b.Size = new Size(size, size);
b.Text = "B";
b.Location = grid[x - 1, y - 1];
btns.Add(b);
}
}
}
Form f = new Form();
f.Size = new Size(1000, 1000);
if (isTest)
{
f.Controls.AddRange(btns.ToArray());
}
else
{
// Add controls to random grid cells
Button b1 = new Button();
b1.Size = new Size(size, size);
b1.Text = "B1";
b1.Location = grid[3, 3];
Button b2 = new Button();
b2.Size = new Size(size, size);
b2.Text = "B2";
b2.Location = grid[5, 5];
Button b3 = new Button();
b3.Size = new Size(size, size);
b3.Text = "B3";
b3.Location = grid[8, 8];
Button b4 = new Button();
b4.Size = new Size(size, size);
b4.Text = "B4";
b4.Location = grid[8, 9];
Button b5 = new Button();
b5.Size = new Size(size, size);
b5.Text = "B5";
b5.Location = grid[9, 8];
Button b6 = new Button();
b6.Size = new Size(size, size);
b6.Text = "B6";
b6.Location = grid[9, 9];
f.Controls.AddRange(new Button[] { b1, b2, b3, b4, b5, b6 });
}
f.ShowDialog();
}
두 번째 contro이되지 않은 것처럼 나타납니다 그래서 그들은 항상 동일한 위치에 표시 컨트롤을 추가 추가되었습니다. 추가 할 때 컨트롤의 위쪽 및/또는 왼쪽 속성을 설정하십시오. – peterG
런타임시 그리드를 적절히 생성하고 있습니까? 'Dim uc As New UserControl'과 같은 속성이 필요합니다 (위의 peterG의 주석 참조). 나중에는 Me.Controls.Add (uc)' –
TableLayoutPanel을 사용자 정의 컨트롤에 넣은 다음 TableLayoutPanel의 다른 섹션에 하위 컨트롤을 배치하는 것이 좋습니다. – Roger