Vb.net 在VB中连接变量名
我有一个大窗口,里面有不到200个控件/变量需要担心。他们中的许多人都很相似,所以我想知道我是否可以把他们的名字连在一起,而不是重复地分别称呼每一个人 我举个例子: 我有5条我担心的数据:红色、橙色、黄色、绿色、蓝色 其中每一个都有一个需要显示的标签、一个需要显示的文本框和一个包含文本框中文本的字符串:Vb.net 在VB中连接变量名,vb.net,winforms,concatenation,Vb.net,Winforms,Concatenation,我有一个大窗口,里面有不到200个控件/变量需要担心。他们中的许多人都很相似,所以我想知道我是否可以把他们的名字连在一起,而不是重复地分别称呼每一个人 我举个例子: 我有5条我担心的数据:红色、橙色、黄色、绿色、蓝色 其中每一个都有一个需要显示的标签、一个需要显示的文本框和一个包含文本框中文本的字符串: lblRed.Visible = True txtRed.Visible = True strRed = txtRed.Text 有没有一种方法可以让我创建某种数组来循环,从而连接这些变量名
lblRed.Visible = True
txtRed.Visible = True
strRed = txtRed.Text
有没有一种方法可以让我创建某种数组来循环,从而连接这些变量名
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
Dim list作为新列表(字符串)(新字符串(){“红色”、“橙色”、“黄色”、“绿色”、“蓝色”})
尺寸i为整数=0
当我
我知道上面的代码不起作用,但我想告诉你我想做什么的基本想法。这看起来可行吗?
使用控件集合:
Dim i As Integer
i = 1
Me.Controls("Textbox" & i).Text = "TEST"
所以
请记住,当连接项时,“+”将作用于字符串而不是整数。您可能希望在连接时始终使用“&”
使用控件集合:
Dim i As Integer
i = 1
Me.Controls("Textbox" & i).Text = "TEST"
所以
请记住,当连接项时,“+”将作用于字符串而不是整数。在连接时,您可能希望始终使用“&”另一种方法是为每种类型的控件使用select case块。大概是这样的:
Private Sub EnableControls()
For Each c As Control In Me.Controls
Select Case c.GetType
Case GetType(TextBox)
c.Visible = True
Select Case c.Name.Substring(3)
Case "Red"
strRed = c.Text
Case "Orange"
strOrange = c.Text
Case "Yellow"
'and so on
End Select
Case GetType(Label)
c.Visible = True
End Select
Next
End Sub
另一种方法是为每种类型的控件使用select case块。大概是这样的:
Private Sub EnableControls()
For Each c As Control In Me.Controls
Select Case c.GetType
Case GetType(TextBox)
c.Visible = True
Select Case c.Name.Substring(3)
Case "Red"
strRed = c.Text
Case "Orange"
strOrange = c.Text
Case "Yellow"
'and so on
End Select
Case GetType(Label)
c.Visible = True
End Select
Next
End Sub
请查看此处的信息: 我相信有人会发现一些比这更雄辩的东西,但这应该会达到你想要的结果。您将需要以下导入:
Imports System.Reflection
如果使用属性访问变量,则可以使用反射按名称获取变量:
'Define these with getters/setters/private vars
Private Property strRed as String
Private Property strOrange as String
Private Property strYellow as String
Private Property strGreen as String
Private Property strBlue as String
For each color as String in list
If Me.Controls.Count > 1 Then
'Should really check for existence here, but it's just an example.
Me.Controls("lbl" & color).Visible = True
Dim tbx as TextBox = Me.Controls("txt" & color)
tbx.Visible = True
Dim propInfo as PropertyInfo = Me.GetType.GetProperty("str" & color)
propInfo.SetValue(Me, tbx.Text, Nothing)
End If
Next
请查看此处的信息: 我相信有人会发现一些比这更雄辩的东西,但这应该会达到你想要的结果。您将需要以下导入:
Imports System.Reflection
如果使用属性访问变量,则可以使用反射按名称获取变量:
'Define these with getters/setters/private vars
Private Property strRed as String
Private Property strOrange as String
Private Property strYellow as String
Private Property strGreen as String
Private Property strBlue as String
For each color as String in list
If Me.Controls.Count > 1 Then
'Should really check for existence here, but it's just an example.
Me.Controls("lbl" & color).Visible = True
Dim tbx as TextBox = Me.Controls("txt" & color)
tbx.Visible = True
Dim propInfo as PropertyInfo = Me.GetType.GetProperty("str" & color)
propInfo.SetValue(Me, tbx.Text, Nothing)
End If
Next