动态分配ControlControl

时间:2015-12-14 10:53:07

标签: c#

我想这样做:

查找picbox [1..n],其中e.Location关闭picbox [1..n] .Location将其分配给this.ActiveControl

但我不知道怎么做?

对于测试我采取第一个元素this.ActiveControl = picBox [0];但我想自动知道这一点。

我可以移动这个物体,看起来不错。

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                MouseDownLocation = e.Location;
                //TODO 
                //Find picbox[1..n] where e.Location is close picbox[1..n].Location assign it to this.ActiveControl 
                this.ActiveControl = picBox[0];
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                this.ActiveControl.Left = e.X + this.ActiveControl.Left - MouseDownLocation.X;
                this.ActiveControl.Top = e.Y + this.ActiveControl.Top - MouseDownLocation.Y;
            }
        }

        static Bitmap[] pictures = new Bitmap[200];
        PictureBox[] picBox = new PictureBox[200];
        int idObjekt;

        private void Form1_Load(object sender, EventArgs e)
        {
            idObjekt = 0;
        }

        private void b_processstart_Click(object sender, EventArgs e)
        {
            pictures[idObjekt] = new Bitmap(@"start.png");
            picBox[idObjekt] = new PictureBox();
            picBox[idObjekt].Location = new System.Drawing.Point(25, 7);
            picBox[idObjekt].SizeMode = PictureBoxSizeMode.StretchImage;
            picBox[idObjekt].ClientSize = new Size(53, 40);
            picBox[idObjekt].Image = pictures[idObjekt];
            picBox[idObjekt].MouseDown += pictureBox1_MouseDown;
            picBox[idObjekt].MouseMove += pictureBox1_MouseMove;
            this.Controls.Add(this.picBox[idObjekt]);
            idObjekt += 1;
        }

1 个答案:

答案 0 :(得分:2)

您可以通过发件人对象访问当前的图片框。

this.ActiveControl = (PictureBox)sender;