I have a list box which displays the usernames on my computer. how do i use the for each statment to pass the usernames so the code can be repeated for each user.
Thanks in advance
Chris1990
Printable View
I have a list box which displays the usernames on my computer. how do i use the for each statment to pass the usernames so the code can be repeated for each user.
Thanks in advance
Chris1990
I don't know how to do it with "For each" but you can use "List1.ListCount" to get the number of items in the listbox, then loop through it like you normally would...
This is what worked for me... might not be the best solution, i dont know... but hey :P
Code:Dim i As Integer
Dim MyArray() As String
Dim lstcount As Integer
lstcount = List1.ListCount
ReDim MyArray(0 To lstcount)
For i = 0 To lstcount - 1
MyArray(i) = List1.List(i)
Next
What code do you need repeated for each username?
I am making this program to add this registry entry for every user on the pc:
SetKeyValue HKEY_LOCAL_MACHINE, "Software\My Apps\Security App\Users\StartUp\" & lblUsername.Caption & "\", REG_DWORD, "load", "0"
otherwise if i dont i get errors of an unsupported value when my program reads it
vb Code:
For i = 0 To List.Count - 1 SetKeyValue HKEY_LOCAL_MACHINE, "Software\My Apps\Security App\Users\StartUp\" & List.Index(i) & "\", REG_DWORD, "load", "0" Next
I would useCode:For i = 0 To List.Count - 1
SetKeyValue HKEY_LOCAL_MACHINE, "Software\My Apps\Security App\Users\StartUp\" & List.List(i) & "\", REG_DWORD, "load", "0"
Next
Keep in mind that the user is going to have to have administrative rights to write to that part of the registry.
thanks,
it reads from the registry by the way
i get an error
"Wrong number of argurements or invalid propery assignment."
this is higlighted
Code:lstUsers.Index(i)
to get the value u need to use
Index is used when the array to list box is used...Quote:
lstUsers.List(i)
This is my code:
it still doesnt add keys to the registryCode:Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Dim SystemPath As String
Dim OS As String
Private Sub Form_Load()
' Get the name of the computer
str_Computer = CreateObject("WScript.Network").ComputerName
' Connect to the computer object
Set oMachine = GetObject("WinNT://" & str_Computer & ",computer")
' Set the filter to list users
oMachine.Filter = Array("user")
' For each user on the machine
For Each oUser In oMachine
' display the user name
lstUsers.AddItem oUser.Name
Next
'--------------------------Set Regkey
Dim lpBuffer As String
Dim nSize As Integer
Dim rc As Long
nSize = 255
lpBuffer = Space$(nSize)
rc = GetSystemDirectory(lpBuffer, nSize)
If (rc <> 0) Then
SystemPath = Left$(lpBuffer, InStr(lpBuffer, Chr$(0)) - 1)
Else
SystemPath = ""
End If
If (Len(SystemPath) = 17) Then
OS = 1 ' windows 98
Else
OS = 2
End If
'---------------------------------------Set RegKey END
'list reg keys
For i = 0 To lstUsers.ListCount - 1
SetKeyValue HKEY_LOCAL_MACHINE, "Software\My Apps\Security App\Users\StartUp\" & lstUsers.List(i) & "\", REG_DWORD, "load", "0"
Next
Try:
(Without the last "\")vb Code:
SetKeyValue HKEY_LOCAL_MACHINE, "Software\My Apps\Security App\Users\StartUp\" & lstUsers.List(i), REG_DWORD, "load", "0"
Thanks, it works now
Chris1990
Why not just use a default value (0, in this case?) when you read the key? Then, if the key doesn't exist, you'll get 0. Only write registry keys that are really needed.