Get access to the Sender control - C#

前端 未结 2 1278
北海茫月
北海茫月 2020-12-21 01:33

How do I get access the sender control (ie: changing is location etc)? I have created some picture boxes at the runtime in a panel set its click event to a function. I want

相关标签:
2条回答
  • 2020-12-21 02:23

    In your click handler, cast the 'sender' parameter to a PictureBox and examine its Location.

    void pb_point_Click(object sender, EventArgs e)
    {
        var pictureBox = (PictureBox)sender;
        MessageBox.Show(pictureBox.Location.ToString());
    }
    
    0 讨论(0)
  • 2020-12-21 02:23

    Sender is your picturebox. Just cast it:

    void pb_point_Click(object sender, EventArgs e)
    {
        var pictureBox = (PictureBox)sender;
        MessageBox.Show(pictureBox.Location.ToString()); //Retrun location of another control.
    }
    
    0 讨论(0)
提交回复
热议问题