简体   繁体   English

vb.net在运行时删除标签和文本框

[英]vb.net deleting a label and textbox at runtime

So i have a class that creates a new label and textbox when it is called. 因此,我有一个在调用时会创建新标签和文本框的类。 The names of each label and textbox are in a continues order counting upward using a variable called i : 每个标签和文本框的名称使用名为i的变量按连续顺序向上i

  • "Label" & i creates the name Label1 and "Textbox" & i creates the name Textbox1 "Label" & i创建名称Label1和"Textbox" & i创建名称Textbox1
  • i += 1

And so on. 等等。 Now i want to add a procedure that deletes the last label and textbox using i . 现在,我想添加一个使用i删除最后一个标签和文本框的过程。 I have tried to make this procedure using the lines of code below but this doesnt work as the string cannot be converted to system.windows.forms.control : 我试图使用下面的代码行进行此过程,但这不能工作,因为无法将字符串转换为system.windows.forms.control:

Form1.Controls.Remove("Label" & i) Form1.Controls.Remove("Textbox" & i) i -= 1

Controls is a list of controls using the line of code: Public controls As List(Of Control) Controls是使用以下代码行的Public controls As List(Of Control)Public controls As List(Of Control)

Basically i need a way to delete a label and textbox using the variable i in the class. 基本上,我需要一种使用类中的变量i删除标签和文本框的方法。 Any ideas? 有任何想法吗? Thanks. 谢谢。

Try... 尝试...

Form1.Controls.RemoveByKey("Label" & i)
Form1.Controls.RemoveByKey("Textbox" & i)

This does not work if you have these controls tucked in GroupBox , Panel , or some other container on your form. 如果将这些控件塞在GroupBoxPanel或窗体上的某些其他容器中,则此方法不起作用。

I think you should try adding your controls into a panel, and then delete it as this link suggests (it's quite like what you tried, but inside a panel): 我认为您应该尝试将控件添加到面板中,然后按照此链接的建议将其删除(这与您尝试过的一样,但是在面板内部):

Removing Dynamically created controls in Panel 在面板中删除动态创建的控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM