vb.net 在 VB 中连接变量名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17578308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:13:28  来源:igfitidea点击:

Concatenate Variable Names in VB

vb.netwinformsconcatenation

提问by Kevin Kesicki

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.

我有一个大窗口,需要担心不到 200 个控件/变量。他们中的许多人都很相似,所以我想知道是否可以将他们的名字连接起来,而不是重复地单独调用每个人。

I'll make an example:

我举个例子:

I have 5 pieces of data that I'm worried about: Red, Orange, Yellow, Green, Blue.

我有 5 条我担心的数据:红、橙、黄、绿、蓝。

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

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?

不是为这 5 条数据中的每一条都列出这个,有没有办法让我可以制作某种数组来循环连接这些变量名?

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?

我知道上面的代码不起作用,但我想告诉你我想要做什么的基本概念。这看起来可行吗?

采纳答案by Jason Bayldon

http://msdn.microsoft.com/en-us/library/7e4daa9c(v=vs.71).aspx

http://msdn.microsoft.com/en-us/library/7e4daa9c(v=vs.71).aspx

Using the controls collection:

使用控件集合:

    Dim i As Integer
    i = 1
    Me.Controls("Textbox" & i).Text = "TEST"

so

所以

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

请记住,在连接项目时,'+' 将适用于字符串而不是整数。您可能希望在连接时始终使用 '&'

回答by tinstaafl

Another way is to use a select-case block for each type of control. Something like this:

另一种方法是为每种类型的控件使用一个 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

回答by casanovabomb

Check out the information here:

查看这里的信息:

http://msdn.microsoft.com/en-us/library/axt1ctd9.aspx

http://msdn.microsoft.com/en-us/library/axt1ctd9.aspx

I'm sure someone may find something slightly more eloquent than this, but this should achieve your desired result. You'll need the below import:

我敢肯定有人可能会发现比这更雄辩的东西,但这应该可以达到您想要的结果。您将需要以下导入:

Imports System.Reflection

If you use properties to access your variables, you can use reflection to grab them by name:

如果您使用属性访问变量,则可以使用反射按名称获取它们:

'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