+ Reply to Thread
Results 1 to 4 of 4

Last list item selected in a Multi-Select list box?

  1. #1
    Brian
    Guest

    Last list item selected in a Multi-Select list box?

    I posted this late Friday with no good responses.

    I'm trying to figure out which item in a multi select list box was last
    selected. Not the last item in the list, but the last item selected.

    For example... A list box with three items "List 1", "List 2", and "List
    3". The user could select "List 3" and then "List 1". I would like the
    algorithm to tell me that on the second Listbox change event... that "List
    1" was selected.

    Then on the flip side.... the logic would have to know whether an item was
    deselected as well.

    I played around with using a couple of collection objects... but couldn't
    come up with a good way to compare the two collections to find the
    difference... which would in theory tell me what object had changed in the
    listbox.

    Any thoughts?

    Thanks,
    Brian



  2. #2
    K Dales
    Guest

    RE: Last list item selected in a Multi-Select list box?

    I am not aware of any way to do this. A listbox does not have any property
    to tell you the chronological order of selection of items - and it may not
    even store the order (all it cares about is the final list of items
    selected). I don't even think you could capture it with any events, since I
    don't think any events fire until the selections are made (but I may be
    wrong... anyone?).

    If the order of selection is important to you, you may have to design your
    own control to capture this - probably would need to combine some existing
    controls with custom code.
    --
    - K Dales


    "Brian" wrote:

    > I posted this late Friday with no good responses.
    >
    > I'm trying to figure out which item in a multi select list box was last
    > selected. Not the last item in the list, but the last item selected.
    >
    > For example... A list box with three items "List 1", "List 2", and "List
    > 3". The user could select "List 3" and then "List 1". I would like the
    > algorithm to tell me that on the second Listbox change event... that "List
    > 1" was selected.
    >
    > Then on the flip side.... the logic would have to know whether an item was
    > deselected as well.
    >
    > I played around with using a couple of collection objects... but couldn't
    > come up with a good way to compare the two collections to find the
    > difference... which would in theory tell me what object had changed in the
    > listbox.
    >
    > Any thoughts?
    >
    > Thanks,
    > Brian
    >
    >
    >


  3. #3
    Brian
    Guest

    Re: Last list item selected in a Multi-Select list box?

    Thanks for the inputs.
    Form the looks of it... you are right... I can't find anything about
    chrono-order of listbox selection. The custome control idea is
    intriguing... but probably more difficult than required for this task.

    I still might spend more time on this collection idea. I think it might
    work... just need to think through it some more.

    Anyway... thanks for the thoughts.

    Brian

    "K Dales" <KDales@discussions.microsoft.com> wrote in message
    news:BF9C8E64-7D37-4230-A15F-89474F15FF8B@microsoft.com...
    > I am not aware of any way to do this. A listbox does not have any

    property
    > to tell you the chronological order of selection of items - and it may

    not
    > even store the order (all it cares about is the final list of items
    > selected). I don't even think you could capture it with any events, since

    I
    > don't think any events fire until the selections are made (but I may be
    > wrong... anyone?).
    >
    > If the order of selection is important to you, you may have to design your
    > own control to capture this - probably would need to combine some existing
    > controls with custom code.
    > --
    > - K Dales
    >
    >
    > "Brian" wrote:
    >
    > > I posted this late Friday with no good responses.
    > >
    > > I'm trying to figure out which item in a multi select list box was last
    > > selected. Not the last item in the list, but the last item selected.
    > >
    > > For example... A list box with three items "List 1", "List 2", and

    "List
    > > 3". The user could select "List 3" and then "List 1". I would like the
    > > algorithm to tell me that on the second Listbox change event... that

    "List
    > > 1" was selected.
    > >
    > > Then on the flip side.... the logic would have to know whether an item

    was
    > > deselected as well.
    > >
    > > I played around with using a couple of collection objects... but

    couldn't
    > > come up with a good way to compare the two collections to find the
    > > difference... which would in theory tell me what object had changed in

    the
    > > listbox.
    > >
    > > Any thoughts?
    > >
    > > Thanks,
    > > Brian
    > >
    > >
    > >




  4. #4
    Jim Cone
    Guest

    Re: Last list item selected in a Multi-Select list box?

    Option Explicit
    Private colOne As VBA.Collection
    Private strItem As String
    'Jim Cone - San Francisco, USA - 12/05/2005
    'Displays a message box with the last item the user selected in
    'a multi-select listbox.
    'Requires a UserForm with two Command buttons and a ListBox.
    'Assumes ListBox is filled using the RowSource property.
    'Code goes in the UserForm code module.

    'Add a caption to this button..."Exit"
    Private Sub CommandButton1_Click()
    Me.Hide
    Set colOne = Nothing
    Unload Me
    End Sub

    'Add a caption to this button..."Last Selected"
    Private Sub CommandButton2_Click()
    MsgBox strItem
    End Sub

    Private Sub ListBox1_Change()
    Dim lngC As Long
    Dim lngTotal As Long
    Dim colTwo As VBA.Collection
    Set colTwo = New Collection
    strItem = "None selected"
    lngTotal = ListBox1.ListCount - 1

    For lngC = 0 To lngTotal
    'Add any new selected items to Collection One.
    On Error Resume Next
    If ListBox1.Selected(lngC) Then
    colOne.Add ListBox1.List(lngC), ListBox1.List(lngC)
    End If
    On Error GoTo 0
    Next 'lngC

    For lngC = 0 To lngTotal
    'Collection Two is empty - add just the currently selected items
    If ListBox1.Selected(lngC) Then
    colTwo.Add ListBox1.List(lngC), ListBox1.List(lngC)
    End If
    Next 'lngC

    'No item was deselected
    If colTwo.Count >= colOne.Count Then
    strItem = colOne.Item(colOne.Count)
    Else
    'An item was deselected
    For lngC = 1 To colOne.Count
    On Error Resume Next
    colTwo.Add colOne.Item(lngC), colOne.Item(lngC)
    If Err.Number = 0 Then
    colOne.Remove colOne.Item(lngC)
    strItem = colOne.Item(colOne.Count)
    Exit For
    End If
    On Error GoTo 0
    Next
    End If

    'Empty the collection - this may not be required
    For lngC = colTwo.Count To 1 Step -1
    colTwo.Remove lngC
    Next
    'This is required.
    Set colTwo = Nothing
    End Sub

    Private Sub UserForm_Initialize()
    Set colOne = New Collection
    End Sub
    '------------------------------------------

    "Brian" <bkstigler@usa.net> wrote in message...
    I posted this late Friday with no good responses.
    I'm trying to figure out which item in a multi select list box was last
    selected. Not the last item in the list, but the last item selected.
    For example... A list box with three items "List 1", "List 2", and "List
    3". The user could select "List 3" and then "List 1". I would like the
    algorithm to tell me that on the second Listbox change event... that "List
    1" was selected.
    Then on the flip side.... the logic would have to know whether an item was
    deselected as well.
    I played around with using a couple of collection objects... but couldn't
    come up with a good way to compare the two collections to find the
    difference... which would in theory tell me what object had changed in the
    listbox.
    Any thoughts?
    Thanks,
    Brian



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1