Yashodhan
Yashodhan

Reputation: 135

Deleting Specific controls in an c# form

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:

    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.
    }

Upvotes: 1

Views: 62

Answers (1)

Mehrzad Chehraz
Mehrzad Chehraz

Reputation: 5137

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.

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

Upvotes: 1

Related Questions