Results 1 to 5 of 5

Thread: ListView Help

  1. #1

    Thread Starter
    Junior Member DanielT's Avatar
    Join Date
    Apr 2011
    Location
    Philippines
    Posts
    16

    Thumbs up ListView Help

    Hello
    Im having problem with the code below...


    Private Sub Command1_Click()
    If Not (ListView.SelectedItem Is Nothing) Then
    ListView.ListItems.Remove (ListView.SelectedItem.Index)
    End If
    End sub



    This code executes even if no item selected in the listview. How do you make it that only selected item in the listview will be deleted.

    Thanks!
    Last edited by DanielT; Jun 18th, 2011 at 04:07 AM.

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: ListView Help

    Try this:
    vb Code:
    1. Private Sub Command1_Click()
    2.    
    3.     '~~~ If there's no items left in the ListView, then we are off..
    4.     If ListView1.ListItems.Count = 0 Then Exit Sub
    5.    
    6.     '~~~ If the item is selected
    7.     If ListView1.SelectedItem.Selected = True Then
    8.         ListView1.ListItems.Remove (ListView1.SelectedItem.Index)
    9.     End If
    10.    
    11. End Sub

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: ListView Help

    Quote Originally Posted by DanielT View Post
    Hello
    Im having problem with the code below...


    Private Sub Command1_Click()
    If Not (ListView.SelectedItem Is Nothing) Then
    ListView.ListItems.Remove (ListView.SelectedItem.Index)
    End If
    End sub



    This code executes even if no item selected in the listview. How do you make it that only selected item in the listview will be deleted.

    Thanks!
    Let me guess that the item that is deleted is the first one that's added to the listview (which may not be at the top of the list if the listview is sorted).

    Your code should work, and the reason it deletes an unexpected record is that the first record added to the list is automatically selected. One way around that is to add a Set ListView.SelectedItem = Nothing line after you are done loading the listview.

  4. #4

    Thread Starter
    Junior Member DanielT's Avatar
    Join Date
    Apr 2011
    Location
    Philippines
    Posts
    16

    Re: ListView Help

    Thanks for the reply akhileshbc ' MartinLiss!

    akhileshbc code is working, but still its deleting unselected record

    @MartinLiss thanks!
    Set ListView.SelectedItem = Nothing 'i think you are correct this might solve the problem

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: ListView Help

    Unfortunately VB6 is one of the few apps my iPhone doesn't have so I can't test this but I think you may have to set seleteditem to nothing after every delete because something will be 'selected'.

Tags for this Thread

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