2011-11-21 3 views
3

보낸 사람 컨트롤에 액세스하려면 어떻게해야합니까 (예 : 위치 변경 등)? 패널의 런타임에 그림 상자를 만든 후 click 이벤트를 함수로 설정했습니다. 사용자가 클릭 한 그림 상자의 위치를 ​​얻고 싶습니다. 나는 또한 this.activecontrol을 시도했지만 작동하지 않으며 폼에 배치 된 컨트롤의 위치를 ​​제공합니다. 다음 코드를 사용하고 있습니다 :보낸 사람 컨트롤에 액세스하십시오 - C#

void AddPoint(int GraphX, int GraphY,int PointNumber) 
    { 
     string PointNameVar = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; 
     string [] PointNameArr = PointNameVar.Split(','); 

     PictureBox pb_point = new PictureBox(); 
     pb_point.Name = "Point"+PointNameArr[PointNumber]; 

     pb_point.Width = 5; 
     pb_point.Height = 5; 
     pb_point.BorderStyle = BorderStyle.FixedSingle; 
     pb_point.BackColor = Color.DarkBlue; 
     pb_point.Left = GraphX; //X 
     pb_point.Top = GraphY; //Y 
     pb_point.MouseDown += new MouseEventHandler(pb_point_MouseDown); 
     pb_point.MouseUp += new MouseEventHandler(pb_point_MouseUp); 
     pb_point.MouseMove += new MouseEventHandler(pb_point_MouseMove); 
     pb_point.Click += new EventHandler(pb_point_Click); 
     panel1.Controls.Add(pb_point); 
    } 


    void pb_point_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show(this.ActiveControl.Location.ToString()); //Retrun location of another control. 
    } 

AddPoint 기능은 X, Y 및 포인트 번호를 제공하는 그림 상자 수를 만들기 위해 루프에 의해 호출됩니다. 코드에 따라 그림 상자는 PointA...PointZ

답변

5

클릭 핸들러에서 '보낸 사람'매개 변수를 PictureBox로 캐스팅하고 위치를 검사합니다.

void pb_point_Click(object sender, EventArgs e) 
{ 
    var pictureBox = (PictureBox)sender; 
    MessageBox.Show(pictureBox.Location.ToString()); 
} 
2

Sender으로 지정됩니다. 그냥 던지기 :

void pb_point_Click(object sender, EventArgs e) 
{ 
    var pictureBox = (PictureBox)sender; 
    MessageBox.Show(pictureBox.Location.ToString()); //Retrun location of another control. 
} 
관련 문제