Results 1 to 10 of 10

Thread: Moving items within listboxes {RESOLVED}

  1. #1

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Arrow Moving items within listboxes {RESOLVED}

    I have two listboxes. At a button click I want to move selected items within these two listboxes. The SelectionMode of both the listboxes is MultipleExtended.

    Pls guide.

    Regards,

    Prakash
    Last edited by pvbangera; Apr 10th, 2004 at 04:53 AM.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Dear pvbangera, before to try to help you, I need to know something more about your target. I presume you want to delete the selected items from listbox1 and add them to listbox2. Am I right? And then, the selected items, must be placed at a specified position, or simply add at the bottom of the list? Bye
    Live long and prosper (Mr. Spock)

  3. #3

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    u had it right. i want to add the items to listbox2 at bottom of the list.

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Dim i As Byte = 0
    Do Until i >= Me.ListBox1.Items.Count
    If Me.ListBox1.GetSelected(i) Then
    Me.ListBox2.Items.Add(Me.ListBox1.Items.Item(i))
    Me.ListBox1.Items.RemoveAt(i)
    Else
    i += 1
    End If
    Loop

    It seems work, but I don't assure it's perfect!

    As you can see, you have to increase the value of the variable "i", only if the item was not removed, because it was removed, the next item was placed in its positin and so, without increase the variable, anyway, you will check the next item !
    Good job!
    Live long and prosper (Mr. Spock)

  5. #5

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    It works fine, I know. But only if there are few items in the list box. I dont want to iterate thru all the items of listbox.

    I am searching for help on ListBox.SelectedIndexCollection and its CopyTo method.

    Can anyone provide with sample code snippet?? MSDN Lib dont have it.

    PS: How to implement "For each" statement in such scenario?

  6. #6
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    This is not exactly what you are looking for, but perhaps it can solve your problem:

    Dim CountDown As Long = 0
    Dim ArrIndici(1000) As Long
    Dim Point As Long = Me.ListBox2.Items.Count
    Dim Count As Long = 0
    Me.ListBox1.SelectedIndices.CopyTo(ArrIndici, 0)
    System.Array.Sort(ArrIndici) ' Ascending
    System.Array.Reverse(ArrIndici) ' Descending
    CountDown = Me.ListBox1.SelectedIndices.Count
    Do Until CountDown <= 0
    Me.ListBox2.Items.Insert(Point, (Me.ListBox1.Items.Item(ArrIndici(Count))))
    Me.ListBox1.Items.RemoveAt(ArrIndici(Count))
    Count += 1
    CountDown -= 1
    Loop

    Oh....it seems to work, but it's not well debugged!
    Good job
    Live long and prosper (Mr. Spock)

  7. #7
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    I had some minutes more and I realized that exists a simpler way. You can't copy selected items in one shot, but you need to iterate so many times as the number of the selected items. Obviously, if you select 95% of a big listbox, this is not useful. Anyway, I think the way: For Each..... don't make this situation different. A difference will be founded, I think, only with a unique command that can copy the collection, but I can't find the right way.

    Dim Fine As Long = Me.ListBox1.SelectedItems.Count
    Dim Conta As Long = 0
    Do While Conta < Fine
    Me.ListBox2.Items.Add(Me.ListBox1.SelectedItems(0))
    Me.ListBox1.Items.Remove(Me.ListBox1.SelectedItems(0))
    Conta += 1
    Loop
    Live long and prosper (Mr. Spock)

  8. #8
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Ufff....I have hardly fight against VB.NET, but now we are able to copy all selected items in one shot! But .....I did not discover how to remove them from listbox1

    Dim pippo(Me.ListBox1.SelectedItems.Count - 1) As Object
    Me.ListBox1.SelectedItems.CopyTo(pippo, 0)
    Me.ListBox2.Items.AddRange(pippo)

    I'm very curios and probably I'll try again, but if you or another friend will discover the right way before me.......please tell me the solution!
    Live long and prosper (Mr. Spock)

  9. #9

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    gr8 work alextyx

    u really worked hard on this query. elephant is thru. only tail is remaining . i'll definetly let u know if i find the solution.

    regards

    prakash

  10. #10

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    Got it. Hope its a better one

    VB Code:
    1. Dim Selection(List1.SelectedItems.Count - 1) As Object
    2.         List1.SelectedItems.CopyTo(Selection, 0)
    3.         List2.Items.AddRange(Selection)
    4.         For Each i As Object In Selection
    5.             List1.Items.Remove(i)
    6.         Next

    Atleast, this will not iterate thru all the items of the List1, but only the selcted items.

    Regards,

    Prakash

    PS: single line command to do so is still not found

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