簡體   English   中英

從 flowlayout 面板的集合中刪除用戶控件本身

[英]Remove user control itself from the collection of flowlayout panel

我制作了一個用戶控件,由 1 個圖片框、帶有“標題”的鏈接標簽標題和帶有“查看”和“刪除”的兩個按鈕標題組成,它看起來像這樣

現在我使用這個用戶控件添加到 flowlayout 面板現在我的問題是如何通過單擊“刪除”按鈕刪除用戶控件本身以便從 flowlayout 面板中刪除自己? 我知道它與 flowLayoutPanel1.Controls.Remove(); 有關。 或 .RemoveAt(); 順便說一句,我正在使用 C# Visual Studio 2013,如果有人能給我至少一個示例代碼,我將不勝感激。

我添加用戶控件的代碼是這個

private void add_Click(object sender, EventArgs e)<br>
{         
     AddItem add = new AddItem();          
     flowLayoutPanel1.Controls.Add(add);                   
}

這對我有用:

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);
}

這應該進入UserControl類中該Button的單擊事件。

更新:

我現在已經養成了讓它更安全的習慣:

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);
}

這樣, PictureBox將永遠不會嘗試顯示已處理的Image並崩潰,即使這種情況只會發生在幾乎為零的時間跨度內。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM