如何通过我的 Windows 窗体应用程序的 FlowlayoutPanel 中的事件处理删除特定面板? [英] How do I remove a specific panel through event handling in my FlowlayoutPanel for my Windows Form Application?

查看:47
本文介绍了如何通过我的 Windows 窗体应用程序的 FlowlayoutPanel 中的事件处理删除特定面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的 C# Windows 窗体应用程序,我创建了一个包含多个面板的 flowlayoutpanel.在面板内,每个面板都有一个清除"按钮.

For my C# Windows Form Application, I have created a flowlayoutpanel that contains several panels. Inside the panel, I have a button "Clear" for each and every single panel.

如何为清除"按钮的代码编写事件处理程序,以便单击该按钮后,该面板将从 flowlayoutpanel 中移除".

How do I write the event handler for the code for the button "Clear" such that once I have click the button, the panel would sort of be "Removed" from the flowlayoutpanel.

这是向flowlayoutpanel添加面板的一小部分代码.

This is a short part of the code of the adding of panels to the flowlayoutpanel.

        nFlowPanel.Controls.Add(createNotificationPanel());
        nFlowPanel.Controls.Add(createNotificationPanel());
        nFlowPanel.Controls.Add(createNotificationPanel());
        nFlowPanel.Controls.Add(createNotificationPanelImpt());
        nFlowPanel.Controls.Add(createNotificationPanelImpt());

这是清除"按钮的代码

Button btnClear = new Button
        {
            Text = "Clear",
            Name = "btnClear",
            Location = new Point(416, 17)
        };
        p.Controls.Add(btnClear);
        btnClear.Click += new EventHandler(buttonClear_Click);

那么我应该在下面的方法中写什么来产生删除的效果,例如在我编写的代码的第一部分中添加的第二个面板?

So what should i write in the following method to have the effect of removing e.g. the second panel that was added in the first part of code I have written?

void buttonClear_Click(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

编辑

创建我的面板的代码是

var p = new Panel 
        {
             BorderStyle = BorderStyle.FixedSingle , 
             Size = new Size(506,100),
             Name = "notifyPanel"
        };

创建我的 FlowLayoutPanel 的代码是

and the code for creating my FlowLayoutPanel is

var nFlowPanel = new FlowLayoutPanel
        {
            FlowDirection = FlowDirection.TopDown,
            WrapContents = false,
            AutoScroll = true,
            Size = new Size(530, 377),
            Location = new Point(13, 145)
        };

我的按钮清除代码是

void buttonClear_Click(object sender, EventArgs e)
    {
        var button = (Control)sender;
        var panel = button.Parent.Controls["notifyPanel"];
        panel.Dispose();
    }

但是它给出了错误你调用的对象是空的.在 panel.Dispose() 行上.

however it gives the error Object reference not set to an instance of an object. on the panel.Dispose() line.

有人可以帮忙吗?

推荐答案

Controls.Remove() 方法非常危险,它不处理控件.它将继续存在,转移到所谓的停车窗口,用完 Windows 和托管资源.在执行此操作不到 10,000 次后,当 Windows 不再愿意让您创建更多窗口时,它会使您的程序崩溃.

The Controls.Remove() method is very dangerous, it doesn't dispose the control. Which will live on, moved to the so-called parking window, using up both Windows and managed resources. After a bit less than 10,000 times doing this it crashes your program when Windows is no longer willing to let you create any more windows.

改为调用控件的 Dispose() 方法.这也会自动从其容器中删除控件.

Call the control's Dispose() method instead. That also automatically removes the control from its container.

void buttonClear_Click(object sender, EventArgs e)
{
    var panel = nFlowPanel.Controls["notifyPanel"];
    panel.Dispose();
}

这篇关于如何通过我的 Windows 窗体应用程序的 FlowlayoutPanel 中的事件处理删除特定面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆