[英]Remove user control itself from the collection of flowlayout panel
I made a user control consist of 1 picture box,linklabel caption with "title" and two buttons caption with "view" and "remove" and it look like this我制作了一个用户控件,由 1 个图片框、带有“标题”的链接标签标题和带有“查看”和“删除”的两个按钮标题组成,它看起来像这样
now I used this user control to add on the flowlayout panel now my question is How do I delete the user control itself by clicking the button "Remove" in order to remove itself from the flowlayout panel?现在我使用这个用户控件添加到 flowlayout 面板现在我的问题是如何通过单击“删除”按钮删除用户控件本身以便从 flowlayout 面板中删除自己? I know it has something to do with flowLayoutPanel1.Controls.Remove();
我知道它与 flowLayoutPanel1.Controls.Remove(); 有关。 or .RemoveAt();
或 .RemoveAt(); btw I'm using C# visual Studio 2013,It would be grateful if someone could give me atleast a sample codes for this one.
顺便说一句,我正在使用 C# Visual Studio 2013,如果有人能给我至少一个示例代码,我将不胜感激。
my code for adding a user control is this one我添加用户控件的代码是这个
private void add_Click(object sender, EventArgs e)<br>
{
AddItem add = new AddItem();
flowLayoutPanel1.Controls.Add(add);
}
This works for me:这对我有用:
private void removeButton_Click(object sender, EventArgs e)
{
// if the images come from unmanged GDI bitmaps
if (this.yourPictureBox.Image != null) this.pb_yourPictureBox.Image.Dispose();
this.Parent.Controls.Remove(this);
}
This should go into the click event of that Button
in your UserControl
class.这应该进入
UserControl
类中该Button
的单击事件。
Update:更新:
I have by now gotten into the habit of making it even a bit more secure:我现在已经养成了让它更安全的习惯:
private void removeButton_Click(object sender, EventArgs e)
{
// if the images come from unmanged GDI bitmaps
if (this.yourPictureBox.Image != null)
{
Image img = pb_yourPictureBox.Image;
this.pb_yourPictureBox.Image = null;
img .Dispose();
}
this.Parent.Controls.Remove(this);
}
This way the PictureBox
will never try to show a disposed Image
and crash, even if that could only happen for a virtually zero timespan..这样,
PictureBox
将永远不会尝试显示已处理的Image
并崩溃,即使这种情况只会发生在几乎为零的时间跨度内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.