Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Programmatically selecting all items in a ListBox

  1. #1

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Resolved [RESOLVED] [2005] Programmatically selecting all items in a ListBox

    Hi there,

    In an attempt to programmatically select all the items in a ListBox, I created the following little algorithm:

    Code:
    Dim index As Integer
    While index < ListBox_Available.Items.Count
        ListBox_Available.SetSelected(index, True)
        index = index + 1
    End While
    As amusing as it is to watch the items get selected one by one as the index is incremented, this isn't exactly what I had in mind. I want to select ALL the items in a list so I can pass it to a generic Sub that handles dealing with the list's selections.

    I found a very convenient ClearSelected method but not its logical counterpart, SelectAll (or something to that effect).

    Has anyone ever attempted to do this? If so, how?

    Yours boggled,
    Mightor

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Programmatically selecting all items in a ListBox

    Check out: SelectionMode

    Set it to MultiSimple for example. You can use loops to select multiple items or just specify a number of items:
    vb Code:
    1. Me.ListBox1.SetSelected(1, True)
    2. Me.ListBox1.SetSelected(3, True)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Programmatically selecting all items in a ListBox

    Yeah, I am busy smacking my forehead off the table as I am typing this (quite a feat, I might add). I would've edited my own post to say I had figured it out if you hadn't already posted the answer. I realised my error about 2 minutes after I had posted. It is now working as intended.

    Thanks though

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [RESOLVED] [2005] Programmatically selecting all items in a ListBox

    No probs. You can loop this way too.
    vb Code:
    1. For i As Integer = 0 To Me.ListBox1.Items.Count - 1
    2.         Me.ListBox1.SetSelected(i, True)
    3.  Next
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [RESOLVED] [2005] Programmatically selecting all items in a ListBox

    I ended up making the sub like this:
    Code:
        Private Sub MoveSelection(ByRef _sourceList As ListBox, ByRef _destList As ListBox, Optional ByVal All As Boolean = False)
            Dim currentServerObj As Server
            If All Then
                While _sourceList.Items.Count > 0
                    currentServerObj = _sourceList.Items(0)
                    _destList.Items.Add(currentServerObj)
                    _sourceList.Items.Remove(currentServerObj)
                End While
    
            Else
                While _sourceList.SelectedItems.Count > 0
                    currentServerObj = _sourceList.SelectedItems(0)
                    _destList.Items.Add(currentServerObj)
                    _sourceList.Items.Remove(currentServerObj)
                End While
            End If
        End Sub
    That way I don't need to select all the items in the list and then move them. I found the action of the selection and the moving of items to be visually very unattractive. With the generic sub i can move items from one list to the other and back by just switching source and dest around.

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