C# 如何动态删除图片盒?

C# 如何动态删除图片盒?,c#,picturebox,C#,Picturebox,我试图让图片盒不断地落在表格中。 这是我试过的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Rain_dropz { public parti

我试图让图片盒不断地落在表格中。 这是我试过的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Rain_dropz
{
    public partial class Form1 : Form
    {
        PictureBox[] RD = new PictureBox[500];
        int ndrop = 0;
        public Form1()
        {
            InitializeComponent();
        }

        public void dropIt()
        {
            for (int i = 0; i < ndrop; i++)
            {
                RD[i].Top += 10;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random rnd = new Random();
            int l = rnd.Next(1,545);
            RD[ndrop] = new PictureBox();
            RD[ndrop].BackColor = System.Drawing.Color.MediumBlue;
            RD[ndrop].Size = new Size(5, 5);
            RD[ndrop].Location = new Point(l, 0);
            this.Controls.Add(RD[ndrop]);
            ndrop++;
            dropIt();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Start();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间Rain_dropz
{
公共部分类Form1:Form
{
PictureBox[]RD=新PictureBox[500];
int ndrop=0;
公共表格1()
{
初始化组件();
}
公共图书馆
{
对于(int i=0;i

我认为最好删除表格中消失的图片框。如何操作?

您可以通过从表单控件列表中删除picturebox来删除它

private void timer1_Tick(object sender, EventArgs e)
    {
        Random rnd = new Random();
        int l = rnd.Next(1,545);
        RD[ndrop] = new PictureBox();
        RD[ndrop].BackColor = System.Drawing.Color.MediumBlue;
        RD[ndrop].Size = new Size(5, 5);
        RD[ndrop].Location = new Point(l, 0);
        RD[ndrop].LocationChanged += pb_LocationChanged;
        this.Controls.Add(RD[ndrop]);
        ndrop++;
        dropIt();
    }


void pb_LocationChanged(object sender, EventArgs e)
    {
        // FORM_LASTBOUND is the Y-Axis point after which you wanted to remove the picturebox.
        if ((sender as PictureBox).Top > FORM_LASTBOUND)
        {
            this.Controls.Remove(sender as PictureBox);
        }

    }