[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
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:
Me.ListBox1.SetSelected(1, True)
Me.ListBox1.SetSelected(3, True)
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 :)
Re: [RESOLVED] [2005] Programmatically selecting all items in a ListBox
No probs. You can loop this way too.
vb Code:
For i As Integer = 0 To Me.ListBox1.Items.Count - 1
Me.ListBox1.SetSelected(i, True)
Next
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.