이 작업을 수행하려면, 당신은 당신의 자신의 일부 컨트롤을 사용할 수 있습니다에 대한 감사 드리겠습니다 그것은에서 할 일괄). 라벨에서 파생
사용자 정의 클래스 :
List<MyLabel> MyLabels = new List<MyLabel>();
public class MyLabel : Label
{
public MyLabel()
{
//Define some default properties
this.TextAlign = ContentAlignment.MiddleCenter;
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Font = new Font("Calibri", 9f, FontStyle.Regular);
this.BackColor = Color.White;
this.ForeColor = Color.Black;
this.Size = new Size(50, 20);
}
}
는 패널 내부 사용자 지정 라벨의 배열 (이 역시 사용자 지정 컨트롤 될 수 있음)에 삽입합니다.
int LeftMargin = 20;
int TopMargin = 20;
int NumberOfLabels = 20;
int LabelsPerRow = 5;
int LabelsDistance = 10;
int _idx = 0;
for (int y = 0; y < (NumberOfLabels/LabelsPerRow); y++)
{
for (int x = 0; x < LabelsPerRow; x++)
{
MyLabels.Add(new MyLabel() { Parent = this.panel1 });
MyLabels[_idx].Location = new Point((LeftMargin + ((MyLabels[_idx].Width + LabelsDistance) * x)),
(TopMargin + ((MyLabels[_idx].Height + LabelsDistance) * y)));
_idx++;
}
}
이
은 결과입니다 : 당신은 단순히 이러한 필드를 변경하는 경우

:

0,123,516 :
int LeftMargin = 1;
int TopMargin = 1;
int NumberOfLabels = 20;
int LabelsPerRow = 20;
int LabelsDistance = 1;
를 당신이 얻을
그런 다음, UI를 채우기 위해 당신의 generator()
메소드를 호출 : 또한 Panel.Controls [] 배열을 통해 레이블 텍스트 속성을 설정할 수 있습니다
generator();
public async void generator()
{
generatedArray = getUniqueRandomArray(1, 81, 20).ToArray();
for (int i = 0; i < MyLabels.Count; i++)
{
for (int j = 1; j < generatedArray[i] + 1; j++)
{
await Task.Run(async() => await Task.Delay(500));
//Set the text property of the custom labels. The order will be respected
MyLabels[i].Text = j.ToString();
}
}
.
this.panel1.Controls[i].Text = j.ToString();
주문도 준수하지만 부모 양식이 닫히면 ArgumentOutOfRange 예외가 발생합니다.
Use thead.Sleep은 UI를 움직이게하는 적절한 방법이 아닙니다. –
@AluanHaddad 알지만, 현재의 문제는 아닙니다. – Syncmaxim
'panel1.Controls'는 멋지게 정렬 된 콜렉션이 아닙니다. 글쎄요,하지만 우리가 사물의 내부 순서를 알고 있을지는 의문입니다. 'getUniqueRandomArray' 인덱스를 기반으로 lblName을 구성하는'panel1.FindControl (lblName)'을 사용하여 레이블 컨트롤을 지능적으로 명명 할 수있는 방법을 제안합니다. – Forty3