Solved

Listview item deletion

Posted on 1998-07-06
5
187 Views
Last Modified: 2010-05-03
How do I swap items from one listview to another without disturbing the indexes. If I remove an item from the bottom of the list.. it is ok, however when I remove an item from the middle of the list I get "Index out of bounds errors". Help !!!! (Show code if possible)
==========================================================
CmdMoveItem_Click()
Dim lngassigned As Long
 
  For lngassigned = 1 To ListAssigned.ListItems.Count
       If ListAssigned.ListItems.Item(lngassigned).Selected = True Then
       ListAssigned.ListItems.Remove lngassigned
       
 End If
 
 
Next lngassigned
0
Comment
Question by:holderb
5 Comments
 

Author Comment

by:holderb
Comment Utility
Edited text of question
0
 
LVL 1

Expert Comment

by:gissing
Comment Utility
E-mail me the code and I'll help you
0
 
LVL 6

Expert Comment

by:anthonyc
Comment Utility
Post the code here so all can help
0
 

Author Comment

by:holderb
Comment Utility
Edited text of question
0
 
LVL 7

Accepted Solution

by:
tward earned 100 total points
Comment Utility
Here is a function and an example of calling it to delete items from a listview:

Public gTotalSelected As Long
Public gSelected() As Long

Public Const LVM_FIRST = &H1000
Public Const LVM_GETSELECTEDCOUNT = (LVM_FIRST + 50)

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Long) As Long

Private Sub btnDeleteSelected_Click()
   
  Call GetSelectedItems(Form1, Form1.ListView1)

  Do While gTotalSelected > 0
         
       Form1.ListView1.ListItems.Remove gSelected(0)
         
       Call GetSelectedItems(Form1, Form1.ListView1)
           
  Loop
   
End Sub

Public Sub GetSelectedItems(ByVal FormToUse As Form, ByVal ListViewControl As Control)

  Dim Counter As Long
  Dim SelectedCount As Long
 
  FormToUse.MousePointer = vbHourglass
  FormToUse.Enabled = False
 
  SelectedCount = 0
  gTotalSelected = SendMessage(ListViewControl.hwnd, LVM_GETSELECTEDCOUNT, 0, 0)
  ReDim gSelected(gTotalSelected) As Long
 
  For Counter = 1 To ListViewControl.ListItems.Count
     
     If ListViewControl.ListItems(Counter).Selected = True Then
       
       gSelected(SelectedCount) = Counter
       SelectedCount = SelectedCount + 1
       
     End If
     
  Next Counter
 
  FormToUse.Enabled = True
  FormToUse.MousePointer = vbDefault
 
End Sub

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now