Sunday, February 16, 2020

[C#][Example] Dynamic create labels

I put these code into .cs file constructor:
string[] pdfs = new string[]{"Apple","Banana","Mango","Cheery","Orange"};
Label[] labels = new Label[pdfs.Length];
for(int i=0; i<pdfs.Length; i++){
  label[i] = new Label();
  label[i].Name = "label"+i;
  label[i].Location = new Point(27,i*30);
  label[i].TabStop = true;
  label[i].Text = pdfs[i];
  this.Controls.Add(labels[i]);
}

Reference
https://stackoverflow.com/questions/15008871/how-to-create-many-labels-and-textboxes-dynamically-depending-on-the-value-of-an

No comments :

Post a Comment