2
Answers

Removing controls dynamically

Ask a question
Aparna

Aparna

16y
3.5k
1

Hi...

I have a windows form that has a combo box,combo1, that contains all the table names of a particular database in access.Now when I select a table name from the combo1 then all the field names of that particular table is shown in another combo box,combo2. Further there is another combo box,combo3, containing some symbols like <, >and = which are added to the combo by hard coding. The combo1 is filled with data on form load and the combo2 is filled on the SelectedIndexChanged event of the combo1. There is another textbox,textbox1 in the form.
Now on a next button click the combo2,combo3 and textbox1 get generated dynamically.Everytime the next button is clicked a new row of these controls get placed on the windows form. The location of these controls are adjusted according to a counter. A remove button also is dynamically created in each row.Now wat I want to do is remove the controls in each row on the click of that row's remove button so that the user can remove the controls on their wish by just clicking the remove button specific to that row.

Please help..I am doing it in windows application.

Part of the code is given below..

void InitializeTableControl()

{

cbx = new ComboBox();

this.Controls.Add(cbx);

cbx.Visible = true;

cbx.Size = new Size(100, 21);

cbx.DataSource = col_Names;

cbx.Location = new Point(140, 71 + count * 50);

cbx.BackColor = Color.Linen;

cbx.ForeColor = Color.Indigo;

cbx.FlatStyle = FlatStyle.Popup;

cbx.Name = "cmbField" + count.ToString();

cby = new ComboBox();

this.Controls.Add(cby);

cby.Visible = true;

cby.Location = new Point(260, 71 + count * 50);

cby.Size = new Size(50, 30);

cby.Items.Add(">");

cby.Items.Add("<");

cby.Items.Add("=");

cby.BackColor = Color.Linen;

cby.ForeColor = Color.Indigo;

cby.FlatStyle = FlatStyle.Flat;

cby.Name = "cmbSign" + count.ToString();

textBoxArr = new TextBox();

this.Controls.Add(textBoxArr);

textBoxArr.Visible = true;

textBoxArr.Location = new Point(340, 72 + count * 50);

textBoxArr.Size = new Size(80, 20);

textBoxArr.BackColor = Color.Linen;

textBoxArr.ForeColor = Color.Indigo;

textBoxArr.Name = "tbCondition" + count.ToString();

btnRemove = new Button();

this.Controls.Add(btnRemove);

btnRemove.Location = new Point(435,71 + count * 50);

btnRemove.Size = new Size(70,23);

btnRemove.Text = "Remove";

btnRemove.BackColor = Color.PapayaWhip;

btnRemove.Click += new EventHandler(btnRemove_Click);

btnRemove.Name = "btnRemove" + count.ToString();

}

private void btnNext_Click(object sender, EventArgs e)

{

count = count + 1;

InitializeTableControl();

}

Thanks in Advance.

Aparna


Answers (2)