访问发件人控件 - C# [英] Get access to the Sender control - C#

查看:108
本文介绍了访问发件人控件 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问发件人控件(即:更改位置等)?我在一个面板的运行时创建了一些图片框,将其点击事件设置为一个功能。我想要获取用户点击的图片框的位置。我也尝试了 this.activecontrol ,但它不工作,并给出了放置在窗体中的控件的位置。我使用以下代码:

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 to get the location of the picturebox clicked by the user. I also tried this.activecontrol but its not working and gives the location of a control placed in the form. I am using the following code:

    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的PictureBoxes ,Y和点数。
根据代码创建的pictureboxes被命名为 PointA ... PointZ

The function AddPoint is called by a loop to create number of PictureBoxes which give X,Y and Point number. According to the code pictureboxes are created are named as PointA...PointZ

推荐答案

在您的点击处理程序中,将sender参数转换为PictureBox并检查其位置。

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());
}

这篇关于访问发件人控件 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆