locked
14 RRS feed

Answers

  • Would that be the case only if the deleted item had been the selected item up until it was removed?  That is to ask, if name1 is selected, and name4 is deleted, should name1 remain selected, or would you want the selection to move to name3?  This might be moot if the deletion mechanism is a context menu or if it acts on the currently selected item.

     

    Without knowing that mechanism, I would do this:

     

    Add a private class variable in the namespace area of the form:

    Code Snippet
    Private LastSelected as Short = 0

     

     

    Then add this handler:

    Code Snippet

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    Dim si As Short = Me.ListBox1.SelectedIndex

    If si = -1 Then

    If Me.LastSelected > 0 Then

    Me.ListBox1.SelectedIndex = Me.LastSelected - 1

    Me.LastSelected = Me.LastSelected - 1

    Else

    If Me.ListBox1.Items.Count > 0 Then

    Me.ListBox1.SelectedIndex = 0

    Me.LastSelected = 0

    End If

    End If

    Else

    Me.LastSelected = si

    End If

    End Sub

     

     

    This appears to work well for single-select listboxes, but I haven't tried it on a multiselect one.
    Sunday, April 13, 2008 4:44 AM

All replies

  • Would that be the case only if the deleted item had been the selected item up until it was removed?  That is to ask, if name1 is selected, and name4 is deleted, should name1 remain selected, or would you want the selection to move to name3?  This might be moot if the deletion mechanism is a context menu or if it acts on the currently selected item.

     

    Without knowing that mechanism, I would do this:

     

    Add a private class variable in the namespace area of the form:

    Code Snippet
    Private LastSelected as Short = 0

     

     

    Then add this handler:

    Code Snippet

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    Dim si As Short = Me.ListBox1.SelectedIndex

    If si = -1 Then

    If Me.LastSelected > 0 Then

    Me.ListBox1.SelectedIndex = Me.LastSelected - 1

    Me.LastSelected = Me.LastSelected - 1

    Else

    If Me.ListBox1.Items.Count > 0 Then

    Me.ListBox1.SelectedIndex = 0

    Me.LastSelected = 0

    End If

    End If

    Else

    Me.LastSelected = si

    End If

    End Sub

     

     

    This appears to work well for single-select listboxes, but I haven't tried it on a multiselect one.
    Sunday, April 13, 2008 4:44 AM
  • What does the user do in the UI to remove an item from the listbox?  I can't reproduce the behaviour you see -- when I remove an item from the list, it selects the one before it.  When I remove the first item, it selects the new first item.

    Sunday, April 13, 2008 11:38 PM