我有這個函數將爲label list
中的所有標籤創建一個Click event
。 label list
是標籤的列表:Click evant出現錯誤:索引超出範圍。必須是非負數且小於集合的大小。參數名稱:索引
Label[] label = new Label[50]; // 50 is only a ramdom number, is not the real number of number of labels
ii
爲label
的number of labels
。
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
。
哪條線拋出異常? – Steve