vb 2010复选框循环

时间:2012-11-01 19:45:44

标签: vb.net string checkbox

所以我正试图让这个权限应用程序继续。我陷入困境,我希望将字符串adkey作为复选框的名称,如“cbo”& ADKEY。这样复选框的名称就是相同的字符串。我有点疯狂,把整个事情复制到这里,这样一团糟。

Dim ADkey As String() =
        {"NoChangingWallpaper", "NoHTMlWallpaper"}
    ' Dim cbo As String = 
    Dim cho As CheckBox
    cho = CType("cbo" & ADkey), CheckBox)
    Dim readvalue = My.Computer.Registry.GetValue(
        "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop", ADkey, Nothing)
    'MsgBox("The value is " & CStr(readValue))
    ' Dim cho(ADkey) As CheckBox
    cho.Name = ADkey
    If readvalue = "1" Then
        cho.Checked = True
    Else
        cho.Checked = False
    End If

msgbox部分用于测试

1 个答案:

答案 0 :(得分:1)

您应该将所有复选框添加到Dictionary(Of String, Checkbox)对象:

Dim ADkey As String() =
    {"NoChangingWallpaper", "NoHTMlWallpaper"}

'This code can move to where the checkboxes are first created, as long as you can reach the variable from here
Dim checkboxes As New Dictionary(Of String, Checkbox) From 
  {
     {"NoChangingWallpaper", cboNoChangingWallpaper},
     {"NoHTMlWallpaper", cboNoHTMLWallpaper}
  }

For Each key As String in ADKey.Where(Function(k) checkboxes.ContainsKey(k))
    Dim regvalue As Boolean = (My.Computer.Registry.GetValue(
    "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop", key, Nothing)="1")
    Dim box As Checkbox = checkboxes(key)
    box.Name = key
    box.Checked = regvalue
Next Key

当我看到这个时,为了避免保留密钥的双重记录,我可能会完全删除字符串数组,并按照以下方式执行:

'This code can move to where the checkboxes are first created, as long as you can reach the variable from here
Dim checkboxes As New Dictionary(Of String, Checkbox) From 
  {
     {"NoChangingWallpaper", cboNoChangingWallpaper},
     {"NoHTMlWallpaper", cboNoHTMLWallpaper}
  }

For Each key As String in checkboxes.Keys
    Dim regvalue As Boolean = (My.Computer.Registry.GetValue(
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop", key, Nothing)="1")
    Dim box As Checkbox = checkboxes(key)
    box.Name = key
    box.Checked = regvalue
Next Key

您是否想要此版本取决于您是否始终查看所有框,或者您是否只想更新某些框。