Solved

Listview item deletion

Posted on 1998-07-06
5
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…

705 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