VB 2005 code problem

Posted on 2007-11-14
Last Modified: 2013-11-26

I am having a problem with the following function that I brought over from VBA into VB 2005
The error is at the line marked 'XXXX and it reads .Selected is not a member of a ListView

    Function sListSelection() As String
        'returns a string separatted by commas of indices selected in a list box
        Dim lngIndex As Long
        Dim S As String
        Dim Count As Long
        Count = 0
        With frmR.lsbActionR
            For lngIndex = 1 To .Items.Count
                If .Selected(lngIndex - 1) Then 'XXXX
                    If Count = 0 Then
                        S = CStr(lngIndex - 1)
                        S = S & "," & CStr(lngIndex - 1)
                    End If
                    Count = Count + 1
                End If
            Next lngIndex
        End With
        sListSelection = S
    End Function
Question by:Murray Brown
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
LVL 43

Accepted Solution

TimCottee earned 500 total points
ID: 20281202
The best way to do this is to use the .SelectedIndices property of the listview which returns a collection containing the selected indices.

Now you could transform that to a string in a similar way:

    Function sListSelection() As String
        'returns a string separatted by commas of indices selected in a list box
        Dim S As String
        With frmR.lsbActionR
            For Each SelectedIndex As Integer In .SelectedIndices
                        S = S & "," & CStr(SelectedIndex)
        End With
        Return S.Substring(1)
    End Function

Or work directly with the collection.
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20281212
try with:
If .Items(IngIndex).Selected Then

but will be better:
Dim Index as Integer
For lngIndex = 1 To .SelectedIndices.Count
                   Index = SelectedIndices(IngIndex)
                    If Count = 0 Then
                        S = CStr(Index - 1)
                        S = S & "," & CStr(Index - 1)
                    End If
                    Count = Count + 1
Next lngIndex

Featured Post

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

630 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