Results 1 to 23 of 23

Thread: [RESOLVED] For X = 0 To List1.ListCount - 1

  1. #1

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Resolved [RESOLVED] For X = 0 To List1.ListCount - 1

    Hello everyone,

    I am facing a problem with the following project:

    in my project i have a Listbox with the Style = Checked,
    The idea is to execute a task on every item in the List.

    I wrote the following to delete each item found if Checked:

    For X = 0 To List1.ListCount - 1
    If List1.Selected(X) = True Then
    List1.RemoveItem List1.ListIndex
    End If
    Next X

    But an error occured, because each time the statement find
    an item checked, the listcount get decreased by 1 everytime.

    So far i have made only this to set attributes to many files at once.
    This project doesn't have Style = Checked, this is normal.

    Dim ctr As Integer
    For ctr = 0 To List1.ListCount - 1
    SetAttr List1.List(ctr), vbReadOnly
    Next

    And work very well, then i tried to use this same code to lock files,
    But i could not, for some reason it only lock the first item but not
    the rest of the items in the listbox.

    I would like to use this same method to do just about anything when
    dealing with a listbox in my project.

    Here is another example:
    I made a little project with a listbox, the idea was to check for some
    Registry values, if found, they will be added to the listbox, the next
    task was to delete those values or to change them all, depending on what
    value for each value, but since they were to many, i did not know what to
    add in the code:

    Dim ctr As Integer
    For ctr = 0 To List1.ListCount - 1
    ===> I meant here <===
    Next

    what is the appropriate code to do these three tasks?

    Thank you all in advance

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: For X = 0 To List1.ListCount - 1

    Try looping from Bottom to Top:
    Code:
      Dim ctr As Integer
      For ctr = List1.ListCount - 1 To 0 Step -1
        List1.RemoveItem ctr
      Next
    ...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: For X = 0 To List1.ListCount - 1

    Thanks akhileshbc,

    But what i meant was, on the first project.
    Not just to remove the item from the listbox,
    But run the task assigned to that item checked.

    and by the way, your code doesn't remove the items checked,
    it remove them all!
    Last edited by C0der; Mar 21st, 2010 at 06:54 AM.

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: For X = 0 To List1.ListCount - 1

    Like this...??? :
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
      MsgBox "Number of items check marked: " & List1.SelCount  '~~~> Display the total number of items checked
      
      Dim ctr As Integer
      For ctr = List1.ListCount - 1 To 0 Step -1  '~~~> Loop through the items
        If List1.Selected(ctr) = True Then        '~~~> Check if the item is checked
          List1.RemoveItem ctr                    '~~~> Then, remove it from the list
        End If
      Next
    End Sub
    
    Private Sub Form_Load()
    '// Assuming that List1's style property is 1-Checkbox
      '~~~ Sample data
      List1.AddItem "a"
      List1.AddItem "b"
      List1.AddItem "c"
      List1.AddItem "d"
      List1.AddItem "e"
    End Sub
    ...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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

    Re: For X = 0 To List1.ListCount - 1

    Code:
    Private Sub Command1_Click()
    Dim i As Long
    For i = List1.ListCount - 1 To 0 Step -1
        If List1.Selected(i) = True Then
           List1.RemoveItem i
        End If
    Next
    End Sub

  6. #6

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: For X = 0 To List1.ListCount - 1

    Thank you again akhileshbc,

    Oh yeah, i do like it a lot, but can i ask you this?
    i am a novice in Visual Basic 6, and what i am really
    looking with this code is to do a task not just removing
    the selected item checked.

    Let me give you an example:
    assuming that i have 45 lines of registry items added in the list1
    then how am i going to add the appropiate code for every indivitual
    line of code in this task?

    For example:

    If Reg.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start") = 2 Then
    List1.AddItem "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start"
    Then the modification code will have to go somewere in that area of the code,
    am i wrong? if yes, how?
    End If

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

    Re: For X = 0 To List1.ListCount - 1

    Your first post asked how to delete items from a checked listbox.

    That has been explained.

    So, if that is your question, then how do you really mean "and what i am really looking with this code is to do a task not just removing the selected item checked."
    Quote Originally Posted by C0der
    assuming that i have 45 lines of registry items added in the list1 then how am i going to add the appropiate code for every indivitual line of code in this task?
    Define "appropriate code"
    Quote Originally Posted by C0der
    If Reg.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start") = 2 Then
    List1.AddItem "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start"
    Then the modification code will have to go somewere in that area of the code, am i wrong? if yes, how?
    End If
    What is a "modification code"?

    You really need to be a lot more specific and detailed on what you are trying to achieve.

    When someone responds to your question, they are going to respond to the question you ask.

  8. #8
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: For X = 0 To List1.ListCount - 1

    Quote Originally Posted by C0der View Post
    Thank you again akhileshbc,

    Oh yeah, i do like it a lot, but can i ask you this?
    i am a novice in Visual Basic 6, and what i am really
    looking with this code is to do a task not just removing
    the selected item checked.

    Let me give you an example:
    assuming that i have 45 lines of registry items added in the list1
    then how am i going to add the appropiate code for every indivitual
    line of code in this task?

    For example:

    If Reg.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start") = 2 Then
    List1.AddItem "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start"
    Then the modification code will have to go somewere in that area of the code,
    am i wrong? if yes, how?
    End If
    You mean like this..????
    Code:
    Private Sub Command1_Click()
      Dim ctr As Integer
      For ctr = List1.ListCount - 1 To 0 Step -1  '~~~> Loop through the items
        If List1.Selected(ctr) = True Then        '~~~> Check if the item is checked
          '//Do whatever you want with the selected list item in here.
          If List1.List(ctr) = "Hello" Then
            MsgBox "Hi how are you?"
          ElseIf List1.List(ctr) = "Your Name" Then
            MsgBox "My name is Akhilesh"
          ElseIf List1.List(ctr) = "your likes" Then
            MsgBox "VB programming"
          End If
          '// Thus, we had done with whatever we want. Now remove the item from the list.
          List1.RemoveItem ctr                    '~~~> Then, remove it from the list
        End If
      Next
    End Sub

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  9. #9

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: For X = 0 To List1.ListCount - 1

    Ok Hack, you right about that, sorry.
    what i meant by moification code was...

    Assuming once again that i have 45 lines of registry items added in the List1
    Then some of these items are gonna be: deleted, removed, modify, add value, etc.

    Something like this for example:

    If Reg.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start") = 2 Then
    List1.AddItem "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start"
    Code here, either to delete, remove, modify, etc.
    then this must be called when this item is checked in the list1
    End If

    Then
    Reg.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start", 4

    Or
    Reg.RegDelete "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start"

    I meant, what about the 45 items added to the list1, how to deal with all of then since
    not all of then will have the same task, some of then will be delete, remove, modufy, etc.

    I don't know if am gonna add every invoke to the same code to all the 45 tasks to this
    single code:

    Dim ctr As Integer
    For ctr = List1.ListCount - 1 To 0 Step -1
    If List1.Selected(ctr) = True Then
    List1.RemoveItem ctr
    End If
    Next

  10. #10
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: For X = 0 To List1.ListCount - 1

    Quote Originally Posted by C0der View Post
    Ok Hack, you right about that, sorry.
    what i meant by moification code was...

    Assuming once again that i have 45 lines of registry items added in the List1
    Then some of these items are gonna be: deleted, removed, modify, add value, etc.

    Something like this for example:

    If Reg.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start") = 2 Then
    List1.AddItem "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start"
    Code here, either to delete, remove, modify, etc.
    then this must be called when this item is checked in the list1
    End If

    Then
    Reg.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start", 4

    Or
    Reg.RegDelete "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start"

    I meant, what about the 45 items added to the list1, how to deal with all of then since
    not all of then will have the same task, some of then will be delete, remove, modufy, etc.

    I don't know if am gonna add every invoke to the same code to all the 45 tasks to this
    single code:

    Dim ctr As Integer
    For ctr = List1.ListCount - 1 To 0 Step -1
    If List1.Selected(ctr) = True Then
    List1.RemoveItem ctr
    End If
    Next
    I don't know whether it's clear to me or not...! But...
    If you have added all 45 registry paths in the Listbox, then you could manipulate the selected items using 3 commandbuttons. Say, Readbutton (reads the value), Modifybutton(modifies the value) and Deletebutton (delete the value).

    Example:
    Code:
    Private Sub Command1_Click() '~~~~ Read button
      Dim ctr As Integer
      For ctr = List1.ListCount - 1 To 0 Step -1  '~~~> Loop through the items
        If List1.Selected(ctr) = True Then        '~~~> Check if the item is checked
          MsgBox Reg.RegRead(List1.List(ctr))     '~~~> Read the value
          List1.RemoveItem ctr                    '~~~> Then, remove it from the list
        End If
      Next
    End Sub
    
    Private Sub Command1_Click() '~~~~ Write button
      Dim ctr As Integer
      For ctr = List1.ListCount - 1 To 0 Step -1  '~~~> Loop through the items
        If List1.Selected(ctr) = True Then        '~~~> Check if the item is checked
          Reg.RegWrite List1.List(ctr)            '~~~> Write the value
          List1.RemoveItem ctr                    '~~~> Then, remove it from the list
        End If
      Next
    End Sub
    
    Private Sub Command1_Click()  '~~~~ Delete button
      Dim ctr As Integer
      For ctr = List1.ListCount - 1 To 0 Step -1  '~~~> Loop through the items
        If List1.Selected(ctr) = True Then        '~~~> Check if the item is checked
          Reg.RegDelete List1.List(ctr)           '~~~> Delete the value
          List1.RemoveItem ctr                    '~~~> Then, remove it from the list
        End If
      Next
    End Sub
    ...

    Edit:
    Don't copy-paste the code. Read it and try to understand it. The Registry manipulation code that I had included might not work. Change it accordingly...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  11. #11

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: For X = 0 To List1.ListCount - 1

    OK, let me start from scratch.
    I have the fallowing project:
    Form1, List1, and a Command1.

    To this project i added the code you give me:

    Option Explicit

    Private Sub Command1_Click()
    Dim ctr As Integer
    For ctr = List1.ListCount - 1 To 0 Step -1
    If List1.Selected(ctr) = True Then
    List1.RemoveItem ctr
    End If
    Next
    End Sub

    Now the purpose of this project is to remove and edit the Registry.
    Nex i will add some lines of code to read some roots in the Registry
    Some will be to read, edit, or delete, sub or values in the Registry.

    The problem i encounter with this is when using the Listbox to run the
    task that will be executing every action to every of the 45 tasks in my project.
    So this event will have to be added on Form_Load

    Private Sub Form_Load()
    Let's do it only with one for now.
    If Reg.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start") = 2 Then
    List1.AddItem "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start"
    End If
    End Sub

    Then on the List1 the fallowing item will be added:
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start

    Then when this item get selected in the List1 and hit the Command1
    This code will be called:
    Reg.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start", 4

    My question is, if i will be dealing with 45 items, how am i suppose to
    accomplish all of this with this code?

    Dim ctr As Integer
    For ctr = List1.ListCount - 1 To 0 Step -1
    If List1.Selected(ctr) = True Then
    List1.RemoveItem ctr
    End If
    Next

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

    Re: For X = 0 To List1.ListCount - 1

    Maybe you are after this

    Reg.RegWrite List1.List(ctr), 4

  13. #13

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: For X = 0 To List1.ListCount - 1

    Hello MarkT,

    Dim ctr As Integer
    For ctr = List1.ListCount - 1 To 0 Step -1
    If List1.Selected(ctr) = True Then

    List1.RemoveItem ctr '<== Remove Checked Boxes
    Reg.RegWrite List1.List(ctr), 4 '<== Edit Registry Values
    Reg.RegDelete List1.List(ctr) '<== Delete Registry Values

    'So what ever other task could be added here?

    Open List1.List(ctr) For Binary As #1 <== 1
    Lock #1 <== 2
    'I also tried to add this, but didn't work, could it be becasue of the two lines?
    'I get an error
    'Run-time error '55':
    'File already open
    End If
    Next

    List1.RemoveItem ctr
    Reg.RegWrite List1.List(ctr), 4
    Reg.RegDelete List1.List(ctr)

    So this three task should do the work right?
    Last edited by C0der; Mar 21st, 2010 at 09:38 AM.

  14. #14

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: For X = 0 To List1.ListCount - 1

    I get an error message:

    Run-time error '-2147467261 (80004003)':

    Method 'RegWrite' of object 'IWshShell3' failed


    The error points to this line:
    Reg.RegWrite List1.List(ctr), 4

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

    Re: For X = 0 To List1.ListCount - 1

    If

    Reg.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start", 4

    works and List.List(ctr) equals HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Alerter\Start then

    Reg.RegWrite List1.List(ctr), 4

    should also work

  16. #16

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: For X = 0 To List1.ListCount - 1

    Unfortunately no matter how i try, it does not work.
    Please help me

  17. #17

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: For X = 0 To List1.ListCount - 1

    Hey guys, i want to thank you all for your cooperation and assistance,
    But the truth is that i really want to get to the bottom of this code.

    I have tried every means possible and nothing seems to work with the code.
    Just to give you a demonstration, i tried the code with something simple...
    i tested the code with a simple task like: Kill+Path = Kill List1.List(ctr)
    and yeah, didn't work for me.

    The only-one that worked was the one that remove the selected item checked.

    I will appreciate if there is somebody that can help me to put all this together.

    Thank you again

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

    Re: For X = 0 To List1.ListCount - 1

    I'm not sure what to suggest since I really don't understand what you are trying to accomplish. If you can restate your purpose more clearly and post the code (complete) that you are trying to accomplish it with then maybe you can get more help.

  19. #19

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: For X = 0 To List1.ListCount - 1

    Everything is very clear Mark, but i know nobody like to read and read,
    but if you re-read my whole post, maybe or probably you understand it.
    All i need is the use of the code posted by akhileshbc on post #4

    not necessarily have to be assigned to work just with the registry
    as long it work with a listbox.

    the comment about the 45 itmes or lines in the listbox, i made all that up
    i am not planing to use that option till i have all this setup, i was just assuming
    that in case i use it for that.

    but right now what i need is just the listbox to work with multiple files,
    can be drag and drop or simple by using: List1.AddItem "example".

    but the most importan thing will be if the Listbox work very well.
    To be honest i don't know how to put it more clearer for you.

    the project have only two controls, a List1 and Buttom, and nothing else.

    but since the task with the registry is to hard for you let me try something else...
    how about deleting files, yeah the option i mention before, the Kill app.
    to make it more easy i added to the listbox the option to drag and drop,
    so i can make some dummy files in my desktop and then drag and drop in listbox
    the press command buttom and the 4 dummy files i created it will be kill from list1.

    how about that...

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

    Re: For X = 0 To List1.ListCount - 1

    Still not sure if understand but see if this is heading in the right direction. Setup a test directory and see if it works.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim i As Integer
    
        For i = List1.ListCount - 1 To 0 Step -1
            If List1.Selected(i) Then
                Kill List1.List(i)
                List1.RemoveItem (i)
            End If
        Next i
    End Sub
    
    Private Sub Form_Load()
    Dim strFile As String
    Dim strDirectory As String
        'Set to a valid directory
        strDirectory = "c:\New Folder\"
        
        'Load a bunch of file names into a list box
        strFile = Dir(strDirectory & "*.*")
        
        Do While strFile <> ""
            List1.AddItem strDirectory & strFile
            strFile = Dir
        Loop
        
        Command1.Caption = "Delete selected file(s)"
        Command1.Width = 2600
    End Sub

  21. #21

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: For X = 0 To List1.ListCount - 1

    Hey Mark, this time i got it to work fine!

    Dim i As Integer

    For i = List1.ListCount - 1 To 0 Step -1
    If List1.Selected(i) = True Then
    Kill List1.List(i)
    List1.RemoveItem (i)
    End If
    Next i

    i just added the: '= True' to this line of code:
    ==> If List1.Selected(i) = True Then
    So it can check when ever is there a selected item or not.
    and about the rest of the code i don't need it.

    this time it works fine to kill files added to the listbox,
    i'll search more later on to findout the right way to do the same
    with the registry.

    Thank you Mark, akhileshbc, and Hack.

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

    Re: For X = 0 To List1.ListCount - 1

    True is not needed

    List1.Selected(i) = True
    and
    List1.Selected(i)

    both evaluate to true so the block inside the if statement will run.

  23. #23

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: For X = 0 To List1.ListCount - 1

    Mark but you know what? ...if i run this code:

    Dim i As Integer
    For i = List1.ListCount - 1 To 0 Step -1
    If List1.Selected(i) Then
    Kill List1.List(i)
    List1.RemoveItem (i)
    End If
    Next i

    it wipe all the items in the Listbox, cheked or unchecked,
    but if i add the one i explain before, then works just fine.

    For i = List1.ListCount - 1 To 0 Step -1
    If List1.Selected(i) = True Then
    Kill List1.List(i)
    List1.RemoveItem (i)
    End If
    Next i

    the diference is just this: = True

    Oh! i get it now, you tried this but in a normal Listbox
    the one i was talking about i added the style = checked

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