问题
When I checked the CheckBoxList Items the dynamic texboxes are added. But on Unchecked I want to remove particular textboxes. The code is working fine for adding textboxes but gives me exception at removing.Any Help will be great.
My Code:
iy = 0;
private void checkedListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (load == false)
{
return;
}
PackingDetails pd = new PackingDetails();
var txt = new TextBox();
for (iy = 0; iy < checkedListBox1.Items.Count; iy++)
if (checkedListBox1.GetItemChecked(iy))
{
txt.Name = iy.ToString();
txt.Text = iy.ToString();
txt.Location = new Point(23, 32 + (iy * 28));
txt.Visible = true;
this.Controls.Add(txt);
break;
}
else if (checkedListBox1.GetItemCheckState(iy) == CheckState.Unchecked)
{
foreach (TextBox t in this.Controls)
{
if (t.Name == iy.ToString())
// here if i skip this if condition the topmost textbox but not the clicked one is deleted.
{
this.Controls.Remove(t);
t.Dispose();
break;
}
}
break;
}
}
OR
I have also tried this
int iy = 0;
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
TextBox txt = new TextBox();
panel1.VerticalScroll.Value = VerticalScroll.Minimum;
if (e.NewValue == CheckState.Checked)
{
txt.Name = iy.ToString();
txt.Text = iy.ToString();
txt.Location = new Point(23, 32 + (iy * 28));
txt.Visible = true;
this.Controls.Add(txt);
count = iy;
iy++;
}
else
{
foreach (Control control in this.Controls)
{
if (control is TextBox)
{
// here it deletes the topmost textbox but not the clicked one. Here if I used if condition like control.Name == iy.ToString() Nothing happens
this.Controls.Remove(control);
control.Dispose();
break;
}
}
}
}
回答1:
this.Controls will have multiple control:
else if (checkedListBox1.GetItemCheckState(iy) == CheckState.Unchecked)
{
foreach (Control con in this.Controls)
{
if(con is TextBox)
{
if (t.Name == iy.ToString())
{
this.Controls.Remove(t);
t.Dispose();
break;
}
}
}
break;
}
Also, If it doesn't work, debug your code and see how the control is flowing
回答2:
Assuming I've guessed the right origin of the error:
foreach (TextBox t in this.Controls)
{
if (t.Name == iy.ToString())
{
this.Controls.Remove(t);
t.Dispose();
break;
}
}
Could possibly work, but can also possibly fail. One cannot just assume all Controls
in this.Controls
are of the type TextBox
. Note that this.Controls
will return all Controls
in a specific context (this
).
The solution is to check whether its a TextBox
or not:
foreach (var control in this.Controls)
{
if (control is TextBox)
{
if (t.Name != null && t.Name == iy.ToString())
{
this.Controls.Remove(t);
t.Dispose();
break;
}
}
}
回答3:
Set CheckOnClick
to True
of CheckBoxList
..
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
for (iy = 0; iy < checkedListBox1.Items.Count; iy++)
{
var item = checkedListBox1.Items[iy];
string ctlName = item.ToString();
Control txt = null;
if (checkedListBox1.GetItemChecked(iy))
{
txt = FindControl(ctlName);
if (txt != null)
{
continue;
}
txt = new TextBox();
txt.Name = ctlName;
txt.Text = ctlName;
txt.Location = new Point(150, 32 + (iy * 28));
txt.Visible = true;
this.Controls.Add(txt);
}
else if (checkedListBox1.GetItemCheckState(iy) == CheckState.Unchecked)
{
txt = FindControl(ctlName);
if (txt==null)
{
continue;
}
this.Controls.Remove(txt);
txt.Dispose();
}
}
}
Control FindControl(String ctlName)
{
for (int i = 0; i < this.Controls.Count; i++)
{
if (this.Controls[i].Name==ctlName)
{
return this.Controls[i];
}
}
return null;
}
来源:https://stackoverflow.com/questions/33751178/remove-dynamic-textboxes-on-checkedlistbox-selectedindexchanged