It is necessary to create several (unknown how many) labels and to determine the pressing method for each. All this should happen cyclically.

public Form1() { InitializeComponent(); flag1 = true; ReadXMLDocument("new.xml", "Π£ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΏΡ€ΠΎΠ΅ΠΊΡ‚Π°ΠΌΠΈ"); arrLabel[] la = new arrLabel[arrLength]; int length, floor, column, row; floor = length = 20; column = row = 0; tableLayoutPanel1.AutoScroll = true; tableLayoutPanel1.AutoSize = true; for (int i = 0; i < arrLength; ++i) { la[i] = new arrLabel(result[i], countRes[i]); la[i].lbl.Size = new Size(160, 20); la[i].lbl.Location = new Point(floor, length); la[i].lbl.AutoSize = true; la[i].lbl.Click() = (sender, args) => la[i].action(); tableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; tableLayoutPanel1.Controls.Add(la[i].lbl, column, row); if (floor < 400) { floor += 150; } else length += 20; if (column == 4) { row++; column = 0; } else column++; Console.WriteLine(column + " " + row); } 
  • And how do you create one Label? Do the same in a loop. - VladD
  • That's the problem: I don't know how to do it programmatically, even for one element. If you give a short example I will be very grateful. - Sergey1991
  • one
    var label = new Label(); label.Click += (s,e) => MessageBox.Show("Вуаля!"); - Veikedo
  • I knew that I needed to use + = ... Thank you very much! - Sergey1991
  • Is this construction exactly correct? Displays the error "Click can be only in the left part of the operation + =" - Sergey1991

1 answer 1

Here's an example, from the documentation :

 const double X = 10, startY = 10, labelVerticalSpacing = 20; const int numberOfLabels = 5; for (int i = 0; i < numberOfLabels; i++) { var labelNumber = i; // Π½Π΅ попадаСмся Π½Π° ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΡƒ с Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠ΅ΠΌ var label = new Label() { Location = new Point(X, startY + i * labelVerticalSpacing), Text = "label #" + i }; label.Click += (sender, args) => ProcessLabelClick(labelNumber); this.Controls.Add(label); } void ProcessLabelClick(int labelNumber) { // здСсь ΠΏΡ€ΠΎΠΈΠ·ΠΎΡˆΡ‘Π» ΠΊΠ»ΠΈΠΊ Π½Π° label c Π½ΠΎΠΌΠ΅Ρ€ΠΎΠΌ labelNumber, ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅ΠΌ. } 
  • one
    There is no such example there :) - Veikedo
  • Well, there is half of this example :-) Well I never wrote on Winforms, I will tell you in secret. - VladD
  • Swears that Click can only be on the left, although it is on the left) - Sergey199
  • @ Sergey1991: Strange. Show your code. - VladD
  • Has resulted the code, the most part does not concern a problem. - Sergey1991