locked
SelectedIndexChanged cascades with multiselect .. is there an 'after selected' event RRS feed

  • Spørgsmål

  • Here is the basic problem. When a listview is set to multiselect = true, the SelectedIndexChanged event fires for every row. This is very bad in a situation like the following psuedo code:

    MultiSelect in listView (many items)
     Clear any items that are selected in GridView
     find that item in gridView using a brute force compare
     select it in the gridView

    Since SelectedIndexChanged fires for every item that is being selected in multiple rows, the process happens for every item. This could be so easily overcome if there was an event that fired AFTER the (multi) selection has occured.

    The only HACK I can think of is to user a timer when the selection changes, then go get the items that are now done selecting and select their counter part in the gridview.

    locutus
    15. december 2009 22:58

Svar

  • A ListView has a SelectedItems collection why not wait to process the items until a button is pressed .

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            MessageBox.Show(ListView1.SelectedItems.Count)
    
            For Each I As ListViewItem In ListView1.SelectedItems
    
                'Code for the selected Items
            Next
    
        End Sub

    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    • Markeret som svar af Martin_Xie 22. december 2009 06:42
    16. december 2009 07:31

Alle besvarelser

  • What I settled on was to create a thread that gathers the selected items after sleeping for 200 ms.

        Private Sub lv_Group_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lv_Group.SelectedIndexChanged
            ' problem. Each time a row is selected (in a multiselect) this event fires
            ' causing a massive amount of unneccessary work. If 10 items are selected, we go through here 10 freekin times.
            If Not select_ThreadBusy Then
                ' prevent more than one selection thread
                select_ThreadBusy = True
                Dim selThread As New Thread(AddressOf selectionThread)
                ' waits 200 while the SelectedIndexChanged can finish selecting
                selThread.Start()
            End If
    ....



        Sub selectionThread()
            If lv_Group.InvokeRequired Then
                Dim npd As noParamDelegate = New noParamDelegate(AddressOf selectionThread)
                lv_Group.Invoke(npd)
            Else
                Application.DoEvents()
                Thread.Sleep(200)
                Dim itemsSelected As ListView.SelectedListViewItemCollection

    ....
    15. december 2009 23:44
  • A ListView has a SelectedItems collection why not wait to process the items until a button is pressed .

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            MessageBox.Show(ListView1.SelectedItems.Count)
    
            For Each I As ListViewItem In ListView1.SelectedItems
    
                'Code for the selected Items
            Next
    
        End Sub

    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    • Markeret som svar af Martin_Xie 22. december 2009 06:42
    16. december 2009 07:31
  • Here is something else you might try . When you release the control key which you must press for a multiselect this event handler will execute . The limitation is that you must use the control key while selecting or press and release it for a single item

     
        Private Sub ListView1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView1.KeyUp
            If e.KeyData = 17 Then
    
                MessageBox.Show(ListView1.SelectedItems.Count)
    
                For Each I As ListViewItem In ListView1.SelectedItems
    
                    'Code for the selected Items
                Next
            End If
            ListView1.SelectedItems.Clear() ' unselect the items
        End Sub

    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    16. december 2009 07:39
  • You are using mutlithreading in an ASPNET application?

    Try to avoid that, I would use Ajax or use an ack button in your case.


    Success
    Cor
    16. december 2009 07:45
  • You can also use the KeyPress event to ckeck for the enter key

        Private Sub ListView1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ListView1.KeyPress
            If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
    
                MessageBox.Show(ListView1.SelectedItems.Count)
    
                For Each I As ListViewItem In ListView1.SelectedItems
    
                    'Code for the selected Items
                Next
            End If
            ListView1.SelectedItems.Clear() ' unselect the items
    
        End Sub

    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    16. december 2009 07:53
  • I choose this workaround:

    wrote the eventhandler and hooked it to KeyUp and MouseUp event.

    This should do for the most Forms-Applications.

    28. august 2020 14:51