GrayFullBuster
GrayFullBuster

Reputation: 1063

How to remove dynamically created textbox and labels in C# Windows Forms?

I want to know how can I remove dynamically created textboxes and labels, I want to remove all dynamically created textboxes and labels from this event cmbMethodActions_SelectedIndexChanged. Because all of the textboxes and labels is based from database. Every the event changes, the textboxes and labels will change too.

As of now here is my code:

private void cmbMethodActions_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetActionFields(int.Parse(cmbMethodActions.SelectedValue.ToString()));
    }

    private void GetActionFields(int i)
    {
        MySqlCommand cmd = new MySqlCommand("call GetActionField("+i+")", cn);
        MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adapter.Fill(dt);

        for (int x = 0; x < dt.Rows.Count; x++)
        {
            TextBox txtfields = new TextBox();
            txtfields.Name = dt.Rows[x]["field_name"].ToString();
            txtfields.Width = 175;
            flowLayoutPanelText.Controls.Add(txtfields);

            Label txtlbl = new Label();
            txtlbl.Name = dt.Rows[x]["field_name"].ToString();
            txtlbl.Text = txtlbl.Name;
            flowLayoutPanelLabel.Controls.Add(txtlbl);
        }
    }

Upvotes: 0

Views: 3466

Answers (1)

Igor
Igor

Reputation: 15893

flowLayoutPanelText.Controls.Clear();
flowLayoutPanelLabel.Controls.Clear();

Upvotes: 3

Related Questions