vahid Araghi
vahid Araghi

Reputation: 136

How to add item right after selected item in listview in VB .NET

I wrote piece of cod for add item right after selected item in listview:

Private Sub InsertBeforeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles InsertBeforeToolStripMenuItem.Click

        
        ListView1.Items.Add(Me.ListView1.Items(ListView1.SelectedIndices(0) + 1).Clone)

        For i = 0 To ListView1.Items.Count - 1

            ListView1.Items(i).Text = i + 1.ToString

        Next
end sub

but its not work this cod add item to end of listview instead of after selected item The question is how can i achive that purpose? thank you all.

Upvotes: 1

Views: 262

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

How about:

If(ListView1.SelectedIndices.Count > 0) Then
  ListView1.Items.Insert(ListView1.SelectedIndices(0) + 1, "new item")
Else
  ListView1.Items.Add("new item")
End If

enter image description here

Upvotes: 1

Related Questions