vb.net 如何在 Visual Basic 2010 中使用变量引用控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16900526/
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 13:50:19  来源:igfitidea点击:

How do I reference controls with a variable in visual basic 2010?

vb.netvariablesreferencecontrols

提问by user2448416

I have 12 checkboxes (32 total, but for now just worrying about the first 12) that are named checkbox1, checkbox 2...checkbox 12. I want a for loop to go through them and see if they are checked. If they are checked, it makes changes to an excel sheet, if not it just continues. I have the logic for the spreadsheet edits and the basic structure of the for loop down, but don't know if there is a way to reference the controls using the counter in the for loop.

我有 12 个复选框(总共 32 个,但现在只关心前 12 个),它们名为 checkbox1、checkbox 2...checkbox 12。我想要一个 for 循环来遍历它们并查看它们是否被选中。如果选中它们,它会更改 Excel 工作表,否则它会继续。我有电子表格编辑的逻辑和 for 循环的基本结构,但不知道是否有办法在 for 循环中使用计数器来引用控件。

For example:

例如:

 for i as integer = 1 to 12
   if ("Checkbox" & i).checked = True Then
       <--Spreadsheet things happen-->
   End if
 then

I have had some people suggest a few things, namely using an array with the checkbox names and then doing checkboxes(i).checked but that leads to quite a few issues. Someone else suggested using controls.containskey and CType but while that doesn't give any compile or run time errors, nothing in the spreadsheet is actually changed and I have no idea what any of what I did means.

我让一些人提出了一些建议,即使用带有复选框名称的数组,然后执行 checkboxes(i).checked 但这会导致很多问题。其他人建议使用 controls.containskey 和 CType ,但是虽然这不会产生任何编译或运行时错误,但电子表格中的任何内容实际上都没有改变,我不知道我所做的任何事情意味着什么。

Does anyone know a simple way of doing this?

有谁知道这样做的简单方法?

回答by xpda

You can access the checkboxes using the Controls collection. However, you need to assign (or convert) from a Control to a Checkbox to access the Checked property, something like this:

您可以使用 Controls 集合访问复选框。但是,您需要将 Control 分配(或转换)到 Checkbox 以访问 Checked 属性,如下所示:

Dim ctl As Control
Dim chk As CheckBox

For Each ctl In Controls
  If TypeOf ctl Is CheckBox Then
    chk = ctl
    If chk.Checked Then MsgBox(chk.Name & "is checked")
    End If
  Next ctl

回答by Idle_Mind

Use the Controls.Find() method:

使用 Controls.Find() 方法:

    Dim matches() As Control
    For i As Integer = 1 To 12
        matches = Me.Controls.Find("CheckBox" & i, True)
        If matches.Length > 0 AndAlso TypeOf matches(0) Is CheckBox Then
            Dim cb As CheckBox = DirectCast(matches(0), CheckBox)
            If cb.Checked Then

            End If
        End If
    Next

回答by Fuzhou Hu

Are you creating the checkboxes in code or in the designer? If in code, add the checkboxes into a Listas you create them. If you're using the designer, add all the checkboxes to a Listin the constructor.

您是在代码中还是在设计器中创建复选框?如果在代码中,List请在创建复选框时将它们添加到 a中。如果您使用设计器,请将所有复选框添加到List构造函数中的 a。

  Private myCheckboxes As New List(Of CheckBox)

  Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    myCheckboxes.Add(checkbox1)
    myCheckboxes.Add(checkbox2)
    '''etc

  End Sub

回答by tinstaafl

Something like this should work:

这样的事情应该工作:

    For Each cb As CheckBox In Me.Controls.OfType(Of CheckBox)()
        If cb.Name.Contains("checkbox") AndAlso cb.Checked Then
            Select Case cb.Name
                Case "checkbox1"
                    'do stuff
                Case "checkbox2"
                    'do stuff
                Case "checkbox3"
                    'do stuff
                    'and so on ...
            End Select
        End If
    Next

if you change your naming system for the checkboxes to include a delimeter(checkbox-1), and make the other checkbox names uniquely different(chckbox13) you could shorten the code this way:

如果您更改复选框的命名系统以包含分隔符(checkbox-1),并使其他复选框名称唯一不同(chckbox13),则可以通过以下方式缩短代码:

    For Each cb As CheckBox In Me.Controls.OfType(Of CheckBox)()
        If cb.Name.Contains("checkbox") AndAlso cb.Checked Then
            edit(cell(2, Integer.Parse(cb.Name.Split("-")(1)) + 7))
        End If
    Next