Solved

Listview item deletion

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 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…

732 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