如何更改for循环中的属性

时间:2013-08-17 07:44:12

标签: vb.net winforms visual-studio-2010

我有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

1 个答案:

答案 0 :(得分:-1)

如果我理解正确,您需要为所有.Valuebuttons)设置一个属性(此处为按钮的swButtons属性)。

声明 您的组件 的数组,而不是 值的数组

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