在VB中串联变量名称
我有一个大窗口,只需要担心不到200个控件/变量.他们中的许多人都是相似的,所以我想知道是否可以将他们的名字串联起来,而不是反复地分别打电话给每个人.
I have a large window with a little under 200 controls/variables to worry about. Many of them are similar, so I am wondering if instead of repeatedly calling each one individually I can concatenate their names.
我将举一个例子:
我担心的5条数据是:红色,橙色,黄色,绿色,蓝色.
I have 5 pieces of data that I'm worried about: Red, Orange, Yellow, Green, Blue.
其中每个将具有一个标签,该标签需要显示为可见,一个文本框需要显示为可见,以及一个字符串,其中包含该文本框中的文本:
Each one of these will have a label that needs to be made visible, a textbox that needs to be made visible, and a string that contains the text in the textbox:
lblRed.Visible = True
txtRed.Visible = True
strRed = txtRed.Text
不是将这5个数据中的每个数据都列出,有没有一种方法可以使我遍历某种数组,从而可以将这些变量名连接起来?
Instead of listing this for every one of those 5 pieces of data, is there a way that I could make some sort of array to loop through that could concatenate these variable names?
Dim list As New List(Of String)(New String() {"Red", "Orange", "Yellow", "Green", "Blue"})
Dim i As Integer = 0
Do while i < list.count
lbl + list(i) + .Visible = True
txt + list(i) + .Visible = True
str + list(i) = txt + list(i) + .Text
i = i+1
Loop
我知道上面的代码行不通,但是我想向您提供我想做的基本想法.这看起来可行吗?
I know that the above code doesn't work, but I wanted to give you the basic idea of what I wanted to do. Does this look feasible?
http://msdn.microsoft.com/zh-cn/library/7e4daa9c(v = vs.71).aspx
使用控件集合:
Dim i As Integer
i = 1
Me.Controls("Textbox" & i).Text = "TEST"
如此
Me.controls("lbl" & list(i)).Visible = true
请记住,在连接项目时,"+"将对字符串起作用,而不是整数.您可能要始终使用'&'串联时
Keep in mind that when concatenating items, '+' will work on strings and not integers. You might want to always use '&' when concatenating