|
-
Jun 18th, 2011, 04:01 AM
#1
Thread Starter
Junior Member
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.
-
Jun 18th, 2011, 04:41 AM
#2
Re: ListView Help
Try this:
vb Code:
Private Sub Command1_Click()
'~~~ If there's no items left in the ListView, then we are off..
If ListView1.ListItems.Count = 0 Then Exit Sub
'~~~ If the item is selected
If ListView1.SelectedItem.Selected = True Then
ListView1.ListItems.Remove (ListView1.SelectedItem.Index)
End If
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,...
-
Jun 18th, 2011, 10:22 AM
#3
Re: ListView Help
 Originally Posted by DanielT
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.
-
Jun 18th, 2011, 09:03 PM
#4
Thread Starter
Junior Member
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
-
Jun 18th, 2011, 10:58 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|