Hi,
The code below selects all items in a listview control,fine,but I was
wondering how I could code to select all but the very last line in the array.

Private Sub SelectAllToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllToolStripMenuItem1.Click

        Dim i As Integer
        Dim lvItm As ListViewItem
        Dim str(2) As String

        Dim oWrite As System.IO.StreamWriter
        oWrite = System.IO.File.CreateText(Me.Text.ToString & ".txt")

        For Each lvItm In ListView2.Items
            str(0) = lvItm.SubItems.Item(0).Text.ToString
            str(1) = lvItm.SubItems.Item(1).Text.ToString
            str(2) = lvItm.SubItems.Item(2).Text.ToString

            oWrite.WriteLine(str(0) & vbTab & str(1) & vbTab & str(2))
        Next

        oWrite.Close()

        ''''''''''''''''''''''''''''''''''''''''''''''''
        ' This Code Selects all in listView control
        ''''''''''''''''''''''''''''''''''''''''''''''''
        For i = 0 To ListView2.Items.Count.ToString - 1
            ListView2.Items(i).Selected = True
            ListView2.Select()
        Next

    End Sub

Cheers & Best Regards

Line 23, change For i = 0 To ListView2.Items.Count.ToString - 1 to For i = 0 To ListView2.Items.Count.ToString [B]- 2[/B]

Thanks,
Would you mind telling me what the -2 is refering too exactly

Since using an Index, the count starts at 0 not 1 and using .Count will count the items from 1 to whatever.
.The -2 will select all items from 0 to -2, which is selecting all items except the last item; last item being .Count - 1 .

-3 will select all items except the last 2 and so on.

EDIT::
I would use:

For i As Integer = 0 To ListView2.Items.Count.ToString - 2

and would declare the Integer all at once. Easier to declare and less typing. :)

Edited 5 Years Ago by codeorder: n/a

Thanks for the explanation,the fog is clearing for this guy!
The last line in the Listview,(in my case) is a statistics calculation
which gives the user an idea as to how long the items (These are tool paths
from a cad/cam system) will take to run in real time.
The problem I have is that an exception is thrown if this last line is selected,
Taking what I already have,is there a way that this last line can never be
selected?

Cheers & Best Regards

Edited 5 Years Ago by jonkeeler: n/a

You can try this:

Private Sub ListView1_ItemSelectionChanged(sender As Object, e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
        With ListView1
            If .Items(.Items.Count - 1).Selected Then .Items(.Items.Count - 1).Selected = False '// unselect last item if .Selected.
            If Not .Focused Then .Focus() '// set focus to ListView if not .Focused.
        End With
    End Sub

...or, when getting values of selected items, you could just check if it is the last item and skip it.

go0d day mrdanny web i have a porblem in listview label and button
i want to use my button to go to the next row of my data in list view
and that data i want it equal to label example index 0 of lisview1 is going to copy in label 1 for example if the data in index 0 is "me" then it will be equal to label the text of label will be "me" to and after i click in the button next i want it to go to next row pls help me sir thank you in advance

Please start a new thread with your question. This thread has been dead for a year, has been marked as solved, and your question does not apply to the original question.

Edited 4 Years Ago by Reverend Jim

This question has already been answered. Start a new discussion instead.