Solved

Listview item deletion

Posted on 1998-07-06
5
189 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA open file from excel cell 4 44
A macro to Count the number of rows across all worksheets 3 128
vb6 connector to SQL Server 2 39
Child Form in front 4 49
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

821 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