Results 1 to 8 of 8

Thread: ListBox.AddRange

  1. #1

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

    Question ListBox.AddRange

    Hi all,

    I want to move selected items from one ListBox to the other. Like
    VB Code:
    1. ListBox2.AddRange(ListBox1.Items)
    will move all items from ListBox1 to ListBox2. Similarly, I want to move only selected (multiple selection) items from ListBox1 to ListBox2.

    Well, I would like to mention that unfortunately
    VB Code:
    1. ListBox2.AddRange(ListBox1.SelectedItems)
    is not working.

    Pls guide.

    Regards,

    Prakash

  2. #2
    Junior Member kylek's Avatar
    Join Date
    Oct 2003
    Posts
    21
    Whats the error that you are getting?

    Instead of what you have, try using
    VB Code:
    1. ListBox2.AddRange(ListBox1.ObjectCollection)
    and
    VB Code:
    1. ListBox2.AddRange(ListBox1.SelectedObjectCollection)
    See if that fixes your problem
    INDEMNITY a legal exemption from liability for damages.
    VBForum Name: kylek
    BLOG | FORUMMARK | VENTURE CREW | DORMLIFE

  3. #3
    New Member
    Join Date
    Dec 2003
    Posts
    12

    check this...

    Private Sub btnMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMove.Click

    Dim i As Integer
    For i = 0 To ListBox1.SelectedIndices.Count - 1

    ListBox2.Items.Add(ListBox1.Items(ListBox1.SelectedIndices(0)))

    ListBox1.Items.Remove(ListBox1.Items(ListBox1.SelectedIndices(0)))

    Next
    End Sub
    Last edited by DigitalHand; Dec 23rd, 2003 at 05:12 PM.

  4. #4

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

    writing code in loop was always easy. thats an orthodox vb6 way to do that. when addrange and removerange methods are given, why not utilise the stuff?

    and kylek, ObjectCollection and SelectedObjectCollection is not working. i tried it earlier also. It gives me error that " 'ObjectCollection' is a type in 'System.Windows.Forms.ListBox' and cannot be used as an expression"

    any further ideas pls??

    regards,

    prakash

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Play around with this code , it's 99 % done with little glitch , though , it might give you a clue .

    VB Code:
    1. Try
    2.  
    3.             For i As Integer = 0 To Me.ListBox1.Items.Count
    4.                 Me.ListBox2.Items.Add(Me.ListBox1.SelectedItems.Item(i))
    5.                 Me.ListBox1.Items.Remove(Me.ListBox1.SelectedItems.Item(i))
    6.  
    7.             Next
    8.  
    9.         Catch ex As Exception
    10.             MsgBox(ex.Message)
    11.         End Try

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    just use an arraylist , one easy step ...
    VB Code:
    1. Dim lbCol As New ListBox.SelectedObjectCollection(ListBox1)
    2.         Dim arr As New ArrayList()
    3.         arr.AddRange(lbCol)
    4.         ListBox2.Items.AddRange(arr.ToArray)
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  7. #7
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    i'm really cooking with gas today , here ya go
    VB Code:
    1. Dim lbCol As New ListBox.SelectedObjectCollection(ListBox1)
    2.         ListBox2.Items.AddRange(ArrayList.Adapter(lbCol).ToArray)
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  8. #8

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

    its working. wot about removing the multi-selected items?? meanwhile i am removing it using loop as .RemoveRange method is not available. any other trick??

    actually i am preparing a form having 2 ListBoxes between which items are selected and removed.

    any help??

    regards,

    prakash

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