Solved

Listview item deletion

Posted on 1998-07-06
5
188 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
ID: 1465043
Edited text of question
0
 
LVL 1

Expert Comment

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

Expert Comment

by:anthonyc
ID: 1465045
Post the code here so all can help
0
 

Author Comment

by:holderb
ID: 1465046
Edited text of question
0
 
LVL 7

Accepted Solution

by:
tward earned 100 total points
ID: 1465047
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

776 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