Results 1 to 7 of 7

Thread: Listview - Select (and stay selected) on click?

  1. #1

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Listview - Select (and stay selected) on click?

    I think im being stupid about this.. or maybe its too early.... lol

    I am trying to make my Listview a select/deselect by click (like checked) so you can select multiple rows without having to hold shift.

    click selects it, click again on that row and it deselects it?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Listview - Select (and stay selected) on click?

    I once needed to implement this using drag. Maybe you could modifies it to suit your needs

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private m_mouseDown As Boolean
    4.  
    5.     Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
    6.         Me.m_mouseDown = True
    7.     End Sub
    8.  
    9.     Private Sub ListView1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
    10.         If Me.m_mouseDown Then
    11.             Dim i = ListView1.HitTest(e.Location).Item.Index
    12.             ListView1.Items(i).Selected = True
    13.         End If
    14.     End Sub
    15.  
    16.     Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
    17.         Me.m_mouseDown = False
    18.     End Sub
    19.  
    20. End Class

  3. #3

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Listview - Select (and stay selected) on click?

    ooo! Hittest! forgot about that.. brb thanks!

    EDIT: it wont "stick" unless i move the mouse off the line...
    Last edited by Static; May 16th, 2013 at 08:16 AM.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Listview - Select (and stay selected) on click?

    I guess creating a List(Of Integer) would be sloppy hmm

  5. #5

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Listview - Select (and stay selected) on click?

    bah.. thought this would work

    Code:
    Private Sub LV1_MouseClick(sender As Object, e As MouseEventArgs) Handles LV1.MouseClick
            Dim ITM As ListViewItem = LV1.Items(LV1.HitTest(e.Location).Item.Index)
            For Each LI As ListViewItem In LV1.SelectedItems
                If LI Is ITM Then LI.Selected = Not LI.Selected
                If LI.SubItems(2).Text = WeekOf.ToString Then LI.Selected = False 'This is making sure you dont pick the current week row...
    
            Next
        End Sub
    but its deselecting the one i just selected...


    also sloppy...
    Code:
    Private Sub LV1_MouseClick(sender As Object, e As MouseEventArgs) Handles LV1.MouseClick
    
            Dim ITM As ListViewItem = LV1.Items(LV1.HitTest(e.Location).Item.Index)
            If ITM.SubItems(10).Text = "0" Then ITM.SubItems(10).Text = "1" Else ITM.SubItems(10).Text = "0"
            For Each LI As ListViewItem In LV1.Items
                If LI.SubItems(10).Text = "1" Then LI.Selected = True Else LI.Selected = False
                If LI.SubItems(2).Text = WeekOf.ToString Then LI.Selected = False : LI.SubItems(10).Text = "0" 'This is making sure you dont pick the current week row...
    
            Next
        End Sub
    but it works... it just seems wrong lol


    EDIT: i didnt even think of using a list! lol thats better than a "flag"

    so creating a boolean list works also...
    Dim ListItemSelected As New List(Of Boolean)

    Code:
    Private Sub LV1_MouseClick(sender As Object, e As MouseEventArgs) Handles LV1.MouseClick
            Dim I As Integer = LV1.HitTest(e.Location).Item.Index
            ListItemSelected(I) = Not ListItemSelected(I)
            For Each LI As ListViewItem In LV1.Items
                LI.Selected = ListItemSelected(LI.Index)
                If LI.SubItems(2).Text = WeekOf.ToString Then LI.Selected = False 'This is making sure you dont pick the current week row...
    
            Next
        End Sub
    but again... seems a bit sloppy lol - better than my idea though!
    Last edited by Static; May 16th, 2013 at 08:45 AM.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Listview - Select (and stay selected) on click?

    Because you deselect it.
    Code:
    If LI Is ITM Then LI.Selected = Not LI.Selected

  7. #7

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Listview - Select (and stay selected) on click?

    yeah... i was thinking the "selection" occurred after the click. nope! lol

    you list idea is the best so far though.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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