0

我有这个功能,它将Click eventlabel list. label list是标签列表:

Label[] label = new Label[50]; // 50 is only a ramdom number, is not the real number of number of labels

iinumber of labelslabel

orar[k]是一个包含一些字段的列表:

static List<Orar> orar = new List<Orar>();

Orar是列表的一个类。ii该列表具有与(标签数)相同的字段数。

这是创建Click事件的代码。

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();
                };
            }

问题是那行不通。我有这个错误:Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

4

2 回答 2

1

我的猜测是你必须为 List 中的 orar 元素分配内存。

所以:

orar.Add( new Orar()); 

然后做你的事情:

orar[i].someProperty = "SetProperty";

编辑: 捕获的变量可能是这里的原因。为我尝试一些东西,然后这样做:

for (int k = 0; k < ii; k++)
            {
                int j = k ; //replace the k's in the lambdas with j  
                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[j].materie;
                    profesor = orar[j].profesor;
                    detali = orar[j].detali;
                    zi = orar[j].zi;  
                    formatmaterie = orar[j].formatmaterie;
                    sala = orar[j].sala;
                    inceputora = orar[j].inceputora;
                    formatOra = orar[j].formatora.ToString();
                    saptamana = orar[j].saptamana;
                    AMPM1 = orar[j].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();
                };
            }
于 2012-10-07T14:18:05.190 回答
0

如果ii = 50然后改变k < ii - 1

于 2012-10-07T14:19:05.977 回答