2013-06-01 2 views
3

"for"를 사용하여 값을 PictureBox의 배열에 지정합니다. 각 PictureBox에 대해 click 이벤트를 제공해야합니다. 클릭하면 클릭 한 PictureBox의 일부 값을 받아야합니다. 그러나 그 배열이기 때문에 클릭 한 PictureBox를 지정하는 "for"카운터의 값을 이벤트로 보내야합니다. 색인).- 값을 보내는 이벤트를 클릭하십시오.

PictureBox[] picboxes = new PictureBox[result]; 
for (int i = 0; i < results; i++) 
{ 
    picboxes[i] = new PictureBox(); 
    picboxes[i].ImageLocation = @FormIni.RetRes((i * 5) + 5 + i); 
    picboxes[i].Click += new System.EventHandler(PictureBoxes_Click); 
} 

private void PictureBoxes_Click(object sender, EventArgs e) 
{ 
    label1.Text = "here I need the value of the picboxes[i] image location"; 
} 

그것은 바보 보일 수있다, 그러나 나는 같은 생각 :

내 코드는 다음과 같습니다

picboxes[i].Click += new System.EventHandler(PictureBoxes_Click(i))

private void PictureBoxes_Click(object sender, EventArgs e, int i)

요약하기를 : 때 나는 PictureBox cr에서 클릭한다. 코드를 통해 배열에 eated, 나는 그것의 가치를받을 필요가 있지만 어떻게? 색인이 필요하기 때문에 배열 피 크 박스 중 하나의 값 (클릭 이벤트 내에서)을 얻는 방법은 무엇입니까?

이것은 큰 문제입니다.

EDIT!

이 질문을 한 후에 찾으십시오.하지만 작동 할 수있는 해결책은 these입니다. 내가 맞습니까?

답변

4

시도를 사용할 수 있습니다이

PictureBox[] picboxes = new PictureBox[result]; 
for (int i = 0; i < results; i++) 
{ 
    picboxes[i] = new PictureBox(); 
    picboxes[i].Name = (i+1).ToString(); 
    picboxes[i].ImageLocation = @FormIni.RetRes((i * 5) + 5 + i); 
    picboxes[i].Click += new System.EventHandler(PictureBoxes_Click); 
} 

private void PictureBoxes_Click(object sender, EventArgs e) 
{ 
    PictureBox p = (PictureBox)sender; 
    string j = p.Name; 
    label1.Text = j; 
} 
0

다음과 같은 (anoynomous 방법) 람다 식

picboxes[i].Click += (sender, eventArguments) => PictureBoxes_Click(sender, eventArguments, i); 
+0

감사를 수행 할 수 있지만, 나를 위해 혼란 그래서 나는 전에 람다 식을 사용한 적이 없으니까. =/ –

+0

내 의견으로는 코딩이 훨씬 쉬워졌습니다. 시도해보고 효과가 있는지 확인하십시오. –

관련 문제