C# 删除动态文本框';选中列表框中的es已选择索引已更改
当我选中复选框列表项时,会添加动态文本框。但在未选中状态下,我想删除特定的文本框。代码在添加文本框时运行良好,但在删除时出现异常。任何帮助都会很好 我的代码:C# 删除动态文本框';选中列表框中的es已选择索引已更改,c#,.net,winforms,checkboxlist,C#,.net,Winforms,Checkboxlist,当我选中复选框列表项时,会添加动态文本框。但在未选中状态下,我想删除特定的文本框。代码在添加文本框时运行良好,但在删除时出现异常。任何帮助都会很好 我的代码: iy = 0; private void checkedListBox_SelectedIndexChanged(object sender, EventArgs e) { if (load == false) { return; }
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())
或
我也试过这个
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;
}
}
}
}
此控件将具有多个控件:
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;
}
此外,如果它不起作用,调试代码并查看控件的运行情况将
复选框列表的复选框单击设置为True
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;
}
private void checkedListBox1\u SelectedIndexChanged(对象发送方,事件参数e)
{
对于(iy=0;iy
假设我猜到了错误的正确来源:
foreach (TextBox t in this.Controls)
{
if (t.Name == iy.ToString())
{
this.Controls.Remove(t);
t.Dispose();
break;
}
}
可能有效,但也可能失败。我们不能假定中的所有控件都属于文本框类型。请注意,this.Controls
将返回特定上下文中的所有控件(this
)
解决方案是检查其是否为文本框
:
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;
}
}
}
请提供特定的异常消息。无效的CastException详细信息无法将类型为“System.Windows.Forms.CheckedListBox”的对象强制转换为类型为“System.Windows.Forms.TextBox”。此错误发生在哪里?@manraj不要对我们发号施令。我们在这里免费帮助你。事实上,这需要我们花时间来帮助,所以你至少可以对社区友好。我有最新的问题。它在checkedListBox1_ItemCheck上也不起作用。您能调试代码并检查控件是否流向:this.Controls.Remove(t);或者不是。我怀疑checkedListBox1的threestate是否设置为true,它的状态可能是中间状态,而不是未经检查的状态。是的,亲爱的。t、 当断点转到内部循环时,Name为null。如果控件通过名称成功添加,请告诉我为什么会发生这种情况。我已更新了项目检查的代码。您是否可以更新此控件的c#代码。不是asp.netIt也是c:P。。好的,我抓到你了。。您正在使用WinForms
。。几分钟后查看我的更新。请检查更改。亲爱的,它不起作用。我已经更新了物品检查的代码。你能更新这个吗?工作正常,无一例外。。。你能具体说明一下吗。。什么不起作用
…不是很有建设性。。它在哪里失败了?上面写着t。名称为空。我已经更新了项目检查的代码。你能更新这个吗?你现在使用的方法是一个非常糟糕的做法。你试过我提供的代码了吗?这应该行的。是的,亲爱的。我用过,但没有得到任何解决方案
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;
}
}
}