在VB中串联变量名称 [英] Concatenate Variable Names in VB

查看:75
本文介绍了在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

这篇关于在VB中串联变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆