这是我刚刚自己写的一个控件,你可以仿照着完善一下。
public class ClosablePictureBox : PictureBox
{
Control parent;
Button b;
const int ButtonSize = 20;
public ClosablePictureBox(Control parent)
: base()
{
this.BackColor = Color.LightBlue;
this.Size = new Size(100, 100);
this.parent = parent;
b = new Button();
b.Text = "X";
b.ForeColor = Color.Red;
b.Size = new Size(ButtonSize, ButtonSize);
parent.Controls.Add(b);
parent.Controls.Add(this);
b.Left = this.Left + this.Width - ButtonSize;
b.Top = this.Top;
b.Click += b_Click;
}
void b_Click(object sender, EventArgs e)
{
parent.Controls.Remove(this);
parent.Controls.Remove(b);
this.Dispose();
b.Dispose();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//用法
private void Form1_Load(object sender, EventArgs e)
{
ClosablePictureBox cp = new ClosablePictureBox(this);
}
}
在picturebox里加个button,然后button触发事件里写picturebox
.visibility=false就完了