删除c#表单中的特定控件

时间:2021-06-29 15:51:19

When I click a button I want to delete that particular flowlayout panel along with the check box and the button itself.But I have no clue how to do this. Here is my code to do this:

当我单击一个按钮时,我想删除该特定的flowlayout面板以及复选框和按钮本身。但我不知道如何做到这一点。这是我的代码:

    private static CheckBox _taskCompletionCheckBox;

    public static void DisplaySingleTask(LayoutType layoutType, FlowLayoutPanel layoutPanel,TodoItem item)
    {
        //creates a panel
        var parentPanel = new FlowLayoutPanel {Parent = layoutPanel, AutoSize = true, BorderStyle = BorderStyle.FixedSingle};

        //Based on layout type, the panel's content's are determined
        switch (layoutType)
        {
            case LayoutType.Small:
                _taskCompletionCheckBox = new CheckBox {Parent = parentPanel, Dock = DockStyle.Left,Text = item.Name,AutoSize = true,BackColor = Color.Transparent};
                _taskCompletionCheckBox.CheckedChanged += checkBox_CheckedChanged;
                _taskCompletionCheckBox.Show();
                var delBtn = new Button { Parent = parentPanel, Dock = DockStyle.Left, Size = new Size(30, _taskCompletionCheckBox.Size.Width),Image = Resources.DeleteTaskImage};
                delBtn.Click += delBtn_Click;

                break;
            case LayoutType.Normal:
                break;
            case LayoutType.Full:
                break;
            default:
                throw new ArgumentOutOfRangeException("layoutType");
        }
    }

    static void delBtn_Click(object sender, EventArgs e)
    {
        //I would like to know how can I get a reference to the the flowlayout panel here so I can call the dispose() method on it.
    }

1 个解决方案

#1


If I have got your point, the FlawLayoutPanel you are looking for is parent of the button. Cast the sender parameter of the event handler to button and get it's parent.

如果我有你的观点,你正在寻找的FlawLayoutPanel是按钮的父级。将事件处理程序的sender参数强制转换为button并获取它的父级。

static void delBtn_Click(object sender, EventArgs e)
{
     Button button = (Button)sender;
     FlowLayoutPanel panel = (FlowLayoutPanel)button.Parent;
     // ..
}

#1


If I have got your point, the FlawLayoutPanel you are looking for is parent of the button. Cast the sender parameter of the event handler to button and get it's parent.

如果我有你的观点,你正在寻找的FlawLayoutPanel是按钮的父级。将事件处理程序的sender参数强制转换为button并获取它的父级。

static void delBtn_Click(object sender, EventArgs e)
{
     Button button = (Button)sender;
     FlowLayoutPanel panel = (FlowLayoutPanel)button.Parent;
     // ..
}