|
-
Apr 8th, 2004, 06:37 AM
#1
Thread Starter
Fanatic Member
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.
-
Apr 8th, 2004, 06:49 AM
#2
Hyperactive Member
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)
-
Apr 8th, 2004, 06:59 AM
#3
Thread Starter
Fanatic Member
u had it right. i want to add the items to listbox2 at bottom of the list.
-
Apr 8th, 2004, 08:35 AM
#4
Hyperactive Member
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)
-
Apr 8th, 2004, 08:49 AM
#5
Thread Starter
Fanatic Member
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?
-
Apr 8th, 2004, 11:44 AM
#6
Hyperactive Member
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)
-
Apr 9th, 2004, 06:31 AM
#7
Hyperactive Member
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)
-
Apr 9th, 2004, 10:05 AM
#8
Hyperactive Member
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)
-
Apr 10th, 2004, 03:14 AM
#9
Thread Starter
Fanatic Member
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
-
Apr 10th, 2004, 04:51 AM
#10
Thread Starter
Fanatic Member
Got it. Hope its a better one
VB Code:
Dim Selection(List1.SelectedItems.Count - 1) As Object
List1.SelectedItems.CopyTo(Selection, 0)
List2.Items.AddRange(Selection)
For Each i As Object In Selection
List1.Items.Remove(i)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|