Results 1 to 14 of 14

Thread: [RESOLVED] for each list item in Listbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Resolved [RESOLVED] for each list item in Listbox

    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
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: for each list item in Listbox

    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
    Last edited by NickThissen; May 1st, 2007 at 05:55 PM.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: for each list item in Listbox

    What code do you need repeated for each username?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: for each list item in Listbox

    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
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  5. #5
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: for each list item in Listbox

    vb Code:
    1. For i = 0 To List.Count - 1
    2. SetKeyValue HKEY_LOCAL_MACHINE, "Software\My Apps\Security App\Users\StartUp\" & List.Index(i) & "\", REG_DWORD, "load", "0"
    3. Next

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: for each list item in Listbox

    I would use
    Code:
    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

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: for each list item in Listbox

    Keep in mind that the user is going to have to have administrative rights to write to that part of the registry.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: for each list item in Listbox

    thanks,

    it reads from the registry by the way
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: for each list item in Listbox

    i get an error
    "Wrong number of argurements or invalid propery assignment."

    this is higlighted

    Code:
    lstUsers.Index(i)
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  10. #10
    Hyperactive Member binilmb's Avatar
    Join Date
    Nov 2005
    Location
    Kochi
    Posts
    472

    Re: for each list item in Listbox

    to get the value u need to use

    lstUsers.List(i)
    Index is used when the array to list box is used...
    ßįňįl

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: for each list item in Listbox

    This is my code:

    Code:
    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
    it still doesnt add keys to the registry
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  12. #12
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: for each list item in Listbox

    Try:
    vb Code:
    1. SetKeyValue HKEY_LOCAL_MACHINE, "Software\My Apps\Security App\Users\StartUp\" & lstUsers.List(i), REG_DWORD, "load", "0"
    (Without the last "\")

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: for each list item in Listbox

    Thanks, it works now

    Chris1990
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  14. #14
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: [RESOLVED] for each list item in Listbox

    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.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width