1
Answer

How to retrieve the adynamically created textboxes and Checkboxes

Kriti

Kriti

14y
2.4k
1

I am using System.Windows.Forms
I need to to dynamically create textbox and checkbox on clicking Add button.
Depending upon how many checkboxes are checked,the texboxes should be deleted (When Delete button is Pressed).
What I have done is I have dynamically created Textbox and checkboxes and stored in an array of control.
But at the time of deleting ,When i am iterating through the array ,Its not deleting the corresponding texbox(for which the checkbox is checked)
Here goes my code
public Control[] arrayOfControl()
{
for (int i = 0; i < 10; i++)
{
arr[i] =
new Control();
}
return arr;
}
//For dynamically creation of textbox and checkbox
private
void btnAdd_Click(object sender, EventArgs e)
{
newtxt =
new TextBox();
newchkbox =
new CheckBox();

newchkbox.Name =
"chk" + i.ToString();
newtxt.Name =
"txt" + i.ToString();
arr[i].Controls.Add(newchkbox);
arr[i].Controls.Add(newtxt);
newchkbox.Location =
new System.Drawing.Point(230, 72 + Position);
newtxt.Location =
new System.Drawing.Point(27, 72 + Position);
newtxt.Size =
new System.Drawing.Size(187, 20);
this.btnAdd.Location = new System.Drawing.Point(27, 109 + Position);
this.btnRem.Location = new System.Drawing.Point(113, 109 + Position);
groupBox1.Controls.Add(newtxt);
groupBox1.Controls.Add(newchkbox);
Position += 30;
i++;

}
 
//For delete
private
void btnRem_Click(object sender, EventArgs e)
{
for (int j = 0; j < i; j++)
{

if (arr[j].Contains(newchkbox))
{
if (newchkbox.Checked == true)
{
groupBox1.Controls.Remove(
this.newtxt);

}
}

}}
But the remove button is not working,I am not able to delete the particular textboxes from which checkboxes are checked..How to achieve that?
Answers (1)
Next Recommended Forum