通过字符串删除控件名称 [英] removing a control via string for name
本文介绍了通过字符串删除控件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在尝试移除动态分配的文本框时遇到了一些麻烦。
这是我到目前为止的原因
私人 Sub TextBox1_Click(< span class =code-keyword> ByVal sender As System。 Object , ByVal e As System.EventArgs)
Dim selected 作为 字符串 = CType ( sender,TextBox).Name
Me .Controls.Remove(selected)
End Sub
它表示无法将字符串转换为文本框。 />
如何转换选定的变量s进入控件名称以从表单中删除它?
解决方案
您不需要获取名称。你已经得到了TextBox,而.Remove()方法正在寻找TextBox,而不是它的名字。
试试
< pre lang =xml> foreach(panel1.Controls.OfType中的控制项< TextBox > ())
{
if(item.name ==...)panel1.Controls.Remove(item);
}
I'm having a little bit of trouble trying to remove a dynamically assigned textbox.
This is what I have so far
Private Sub TextBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim selected As String = CType(sender, TextBox).Name
Me.Controls.Remove(selected)
End Sub
It says cannot convert string to a textbox.
how can I turn the selected variable string into a control name to remove that from the form?
解决方案
You don't need to get the name. You're already getting the TextBox and the .Remove() method is looking for just that, the TextBox, not the name of it.
Try
foreach (Control item in panel1.Controls.OfType<TextBox>()) { if (item.name == "...") panel1.Controls.Remove(item); }
这篇关于通过字符串删除控件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文