2013-01-05 3 views
0

아래의 코드는 버튼을 배열에 넣었을 때 각 버튼의 인덱스를 표시하지만이 모든 것은 폼로드에서 수행됩니다. 그러나 나는 약이 일에 내가 가서 얼마나 클릭 이벤트가 아닌 형태의 부하에 같은 일을하고 싶지 :클릭 이벤트를 사용하여 2D 배열의 인덱스 확인

이미지 : enter image description here 코드

:

namespace _2DArray 
{ 
    public partial class Form1 : Form 
    { 
     private Button[,] b; 
     public Form1() 
     { 
      InitializeComponent(); 
      b = new Button[2, 2]; 
      b = new Button[,] { {button1,button2 }, 
           {button3, button4}}; 
     } 
     public int x = 0; 
     public int y = 0; 
     private void Form1_Load(object sender, EventArgs e) 
     { 
      foreach (Button bt in b) 
      { 
       bt.Click += new System.EventHandler(this.ClickedButton); 
      } 

      for (int i = 0; i < 2; i++) 
      { 
       for (int j = 0; j < 2; j++) 
       { 
        b[i, j].Click += new System.EventHandler(this.ClickedButton); 
        b[i, j].Name = "X: " + i + " " + "Y: " + j; 

       } 
      } 

     } 
     private void ClickedButton(object sender, EventArgs e) 
     { 
      Button s = (Button)sender; 
      MessageBox.Show("you have clicked button: " + s.Name); 


     } 
    } 
} 
+1

'포인터'가 의미하는 바를 정확히 설명하십시오 - 전통적인 의미는 당신이 말하는 것과 다릅니다. – Oded

+0

@Oded 질문을 업데이 트했습니다 ..하지만 내가 무엇을 요구하는지 명확하게 생각합니다 – Tacit

+1

'int.Parse (s.Name.Split (""[1])'및 Y로 X 색인을 얻을 수 있습니다. 'int.Parse (s.Name.Split (""[3])'그것이 원하는 경우. –

답변

0

과 같이 답변 :

private void ClickedButton(object sender, EventArgs e) 
     { 
      Button s = (Button)sender; 
      int x = int.Parse(s.Name.Split()[1]); 
      int y = int.Parse(s.Name.Split()[3]); 

      MessageBox.Show("you have clicked button: " + x +" "+ y); 
     }