2012-11-01 240 views
0

所以我试图让这个权限的应用程序去。我陷入了困境,我想让字符串adkey成为复选框的名称,如“cbo”& adkey。所以相同的字符串将是复选框的名称。我有点生气,把整个事情都抄在了这里,好让它变得一团糟。vb 2010复选框循环

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部分是用于测试

+0

LOL @'我得到了一种疯狂和复制整个事情在这里,所以它的一种混乱' – JonH

+0

你知道你想使用一个数组,就好像它是一个字符串,对不对? –

+0

我曾想过,但我不知道如何链接不同的字符串。我遇到的所有例子都是整数。 – NoNo

回答

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 

你是否想要这个版本取决于你是否总是看着所有的盒子,或者你是否只想更新某些盒子。

+0

这完全有效。非常感谢,你已经做了我的一天:)。 – NoNo