1

我有 12 个controls,我想在 for 循环中更改它们的值。我尝试了很多方法,但我无法解决这个问题。

这是我的代码:

Dim serviceNames() As String = {"Security Manager Remote Recorder", _
  "Security Manager Filter", "Security Manager Prob", _
  "Security Manager Intelligence", "Security Manager Maintenance", _
  "Security Manager Action", "Security Manager Agent Check", _
  "Security Manager Control", "Security Manager Deploy Copy", _
  "Security Manager Monitor", "Security Manager Reloader", _
  "Security Manager Schedule"}
Dim swButtons() = {sw0.Value, sw1.Value, sw2.Value, sw3.Value, sw4.Value, _
  sw5.Value, sw6.Value, sw7.Value, sw8.Value, sw9.Value, sw10.Value, sw11.Value}

For q As Integer = 0 To serviceNames.Count - 1
    Dim regService = My.Computer.Registry.GetValue( _
       "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\" + _
       serviceNames(q) + "", "Start", "")

    If regService = 2 Then
        swButtons(q) = True
    End If

    If regService = 4 Then
        MetroTabItem5.Text = "bu 4"
    End If
Next
4

1 回答 1

-1

如果我理解正确,您想为所有( ) 设置一个属性(这里.Value是按钮的属性)。buttonsswButtons

声明一个组件数组,而不是它们的数组:

Dim swButtons() As DevComponents.DotNetBar.Controls.SwitchButton = _
     {sw0, sw1, sw2, sw3, sw4, sw5, sw6, sw7, sw8, sw9, sw10, sw11}

然后你可以像这样轻松地迭代:

For Each item As DevComponents.DotNetBar.Controls.SwitchButton In swButtons
     item.Value = True
Next

或者,如果您想根据索引更改循环中一个按钮的单个值,q您可以像这样访问该按钮:

swButtons(q).Value = True
于 2013-08-17T10:42:24.040 回答