简体   繁体   English

Visual Studio - 如何将变量用于对象名称?

[英]Visual Studio - How i can use a variable into a object name?

i'm learning Visual, and i'm tryind to do this: 我正在学习视觉,我正在努力做到这一点:

    For Each folder In Dir.Subfolders
        list = list + 1
        C1CheckBox(list).Text = folder.Name
    Next

I have a lot of checkboxex named C1CheckBox1, C1CheckBox2, C1CheckBox3, etc... then i want to change the text of each checkbox by the folder name (using the list var to reference the object)... 我有很多名为C1CheckBox1,C1CheckBox2,C1CheckBox3等的checkboxex ...然后我想用文件夹名称更改每个复选框的文本(使用list var引用对象)...

How i can do this? 我怎么能这样做?

thankyou for read 谢谢你的阅读

You can loop through all of the checkboxes setting their text as you go. 您可以循环浏览所有设置文本的复选框。 See this answer for an example for how to enumerate the checkboxes 有关如何枚举复选框的示例,请参阅此答案

You can find controls by name with Controls.Find : 您可以使用Controls.Find按名称查找控件

For Each folder In Dir.Subfolders
    list = list + 1
    Dim cb As CheckBox = Me.Controls.Find("C1CheckBox" & list, True)(0)
    cb.Text = folder.Name
Next

This will search the entire form including its child containers. 这将搜索整个表单,包括其子容器。 If you know all your checkboxes are in say, panel1, you could be more specific: 如果你知道所有的复选框都在,例如panel1,你可以更具体:

Dim cb As CheckBox = Me.Panel1.Controls.Find("C1CheckBox" & list, False)(0)

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

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