将“控制”动态指定给ActiveControl

本文关键字:ActiveControl 动态 控制 | 更新日期: 2023-09-27 18:26:38

我想这样做:

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

但我不知道怎么做

为了测试,我取第一个元素这个。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;
        }

将“控制”动态指定给ActiveControl

您可以通过sender对象访问当前图片框。

this.ActiveControl = (PictureBox)sender;