【问题标题】:How Can I make string expression variable [duplicate]如何使字符串表达式变量[重复]
【发布时间】:2019-04-24 14:04:30
【问题描述】:

我为电影票务项目制作了一百多个复选框(大学) 我们必须检查选中的复选框。 但我们无法通过。

我想

For i As Integer = 1 To 180 Step 1
If ("checkbox" & i).checked = True Then
    'blah blah blah
End If
Next

我知道这在语法上不正确,但你知道我的意思。 请帮助我们:(我们坚持了很长时间......

ps。当然,我们可以为这个系统开辟另一条路线。 但我想知道它。 谢谢。

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    您可以将所有复选框放在一个组框中,然后为其中的所有复选框创建一个循环,如下所示:

    For Each ctrl In GroupBox1.Controls
    
          If (ctrl.GetType() Is GetType(CheckBox)) Then
              Dim chkbx As CheckBox = CType(ctrl, CheckBox)
          End If
        Next
    

    或者,如果您坚持使用预定义的字符串,请尝试使用直接转换:

    For i as integer = 1 to 180 Step 1
            dim chkbox as Checkbox = DirectCast(Controls("checkbox" & i.ToString), Checkbox )
            If chkbox IsNot Nothing
                ' do something with the object now   
            End If
       Next
    

    【讨论】:

    • 如果在该组框中是另一个控件,例如带有复选框的面板......?您认为您的任何一个解决方案都行得通吗?
    • 从来没有想过这一点,直到合理的尺寸可以用 For Each containertype 首先在 form 中进行硬循环,然后对于 container1-2-3.controls 中的每个 ctrl 我想。或者如果事情变得非常混乱,DirectCast 或 CType 或 Find from id string 将是更好的方法。
    • 感谢您的帮助!我会尽快检查的。
    猜你喜欢
    • 2015-09-11
    • 1970-01-01
    • 2021-05-19
    • 2020-11-12
    • 1970-01-01
    • 2016-11-17
    • 2017-10-27
    • 2011-09-30
    • 1970-01-01
    相关资源
    最近更新 更多