I have this function that will create a Click event for all the labels that are in the label list. label list is a list of labels:
Label[] label = new Label[50]; // 50 is only a ramdom number, is not the real number of number of labels
ii is the number of labels from label.
orar[k] is a list that contains some fields:
static List<Orar> orar = new List<Orar>();
Orar is a class for the list. The list have the same number of fields as ii (number of labels).
This is the code for creating the Click event.
for (int k = 0; k < ii; k++)
{
label[k].Click += (s, e) =>
{
string materie = "", profesor = "", detali = "", zi = "", formatmaterie = "", sala = "", inputbox = "", inceputora = "", formatOra = "", saptamana = "", AMPM1 = "", p1 = "";
int cand = 1, vl1 = 0, apm = 0;
materie = orar[k].materie;
profesor = orar[k].profesor;
detali = orar[k].detali;
zi = orar[k].zi;
formatmaterie = orar[k].formatmaterie;
sala = orar[k].sala;
inceputora = orar[k].inceputora;
formatOra = orar[k].formatora.ToString();
saptamana = orar[k].saptamana;
AMPM1 = orar[k].apm;
vl1 = vl11;
cand = 1;
p1 = "Detali materie !";
apm = 0;
inputbox = Programarii.InputAddOrar.Show(p1, ref materie, ref formatOra, ref sala, ref inceputora, ref formatmaterie, ref profesor, ref zi, ref detali, ref saptamana, ref vl1, ref cand, ref apm, ref AMPM1).ToString();
};
}
The problem is that don’t work. I have this error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.
My guess is you have to allocate memory to the orar elements in List.
So:
Then do your stuff:
EDIT:
Captured variables could be the cause here. Try something for me and do this instead: