שתף באמצעות


Check if there is item selected from listview and then delete it and check if there is item selected from listview and then modify it

Question

Thursday, October 1, 2015 4:13 AM

the below code not correct but I dont know how to modify if

a) Delete row

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        If ListView1.SelectedItems Is Nothing Then
            MsgBox("There is no selection to delete", MessageBoxButtons.OK, MsgBoxStyle.Critical)
        Else
            ListView1.Items.Remove(ListView1.SelectedItems(0))
        End If
    End Sub

b) Modify row

Dim zz As ListViewItem

If ListView1.SelectedItems Is Nothing Then
            MsgBox("There is no selection to modify", MessageBoxButtons.OK, MsgBoxStyle.Critical)
        Else
            zz = ListView1.SelectedItems(0)
            zz.Text = Putext.Text
            zz.SubItems.Item(1).Text = Mxttext.Text
            zz.SubItems.Item(2).Text = Mxbtext.Text
            zz.SubItems.Item(3).Text = Myttext.Text
            zz.SubItems.Item(4).Text = Mybtext.Text
        End If

All replies (11)

Thursday, October 1, 2015 5:12 PM ✅Answered

Below is the correct code:

Modify items:

If IsNothing(Me.ListView1.FocusedItem) Then

            MsgBox("Please select an Item to modify", MessageBoxButtons.OK, MsgBoxStyle.Critical)
        Else
            ListView1.Items(ListView1.FocusedItem.Index).SubItems(0).Text = Putext.Text
            ListView1.Items(ListView1.FocusedItem.Index).SubItems(1).Text = Mxttext.Text
            ListView1.Items(ListView1.FocusedItem.Index).SubItems(2).Text = Mxbtext.Text
            ListView1.Items(ListView1.FocusedItem.Index).SubItems(3).Text = Myttext.Text
            ListView1.Items(ListView1.FocusedItem.Index).SubItems(4).Text = Mybtext.Text
        End If

    End Sub

Delete items:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If IsNothing(Me.ListView1.FocusedItem) Then
            MsgBox("Please select an Item to Delete", MessageBoxButtons.OK, MsgBoxStyle.Critical)
        Else
            Me.ListView1.Items.RemoveAt(Me.ListView1.FocusedItem.Index)
        End If
    End Sub

Regards,

Hany metry


Thursday, October 1, 2015 6:16 AM

Hi Hany,

Check the below example on how to remove and edit a ListViewItem (you have to split the example so you don't edit and remove at the same time):

Private Sub button1_Click(sender As Object, e As EventArgs)
    If listView1.SelectedIndices.Count <= 0 Then
        Return
    End If
    Dim intselectedindex As Integer = listView1.SelectedIndices(0)
    If intselectedindex >= 0 Then
        'Edit a row 
        listView1.Items(intselectedindex).SubItems(0).Text = "New Value"
        'Remove Item
        listView1.Items(intselectedindex).Remove()
    End If
End Sub

Fouad Roumieh


Thursday, October 1, 2015 12:00 PM

Dear Mr Fouad,

That will not solve the problem, because i have many input lines and if there is no selected items then it must not remove or edit any, while your code delete or edit the last input even you are not select any.

Kind regards,

Hany Metry


Thursday, October 1, 2015 12:33 PM | 1 vote

Dear Mr Fouad,

That will not solve the problem, because i have many input lines and if there is no selected items then it must not remove or edit any, while your code delete or edit the last input even you are not select any.

Kind regards,

Hany Metry

You mean you don't understand out how to use Fouad Roumieh's code to do what you want to do?

Fouad does say "(you have to split the example so you don't edit and remove at the same time)". However this may be simpler assuming there are 5 subitems for a ListViews selected index although I didn't try the code.

Private Sub button1_Click(sender As Object, e As EventArgs)
    If listView1.SelectedIndices.Count <= 0 Then
        MsgBox("There is no selection to modify", MessageBoxButtons.OK, MsgBoxStyle.Critical)
    Else
                             Dim intselectedindex As Integer = listView1.SelectedIndices(0)
        'Edit a row 
        listView1.Items(intselectedindex).SubItems(0).Text = Putext.Text
                             listView1.Items(intselectedindex).SubItems(1).Text = Mxttext.Text
                             listView1.Items(intselectedindex).SubItems(2).Text = Mxbtext.Text
                             listView1.Items(intselectedindex).SubItems(3).Text = Myttext.Text
                             listView1.Items(intselectedindex).SubItems(4).Text = Mybtext.Text
                    'Remove Item
        listView1.Items(intselectedindex).Remove()
    End If
End Sub

La vida loca


Thursday, October 1, 2015 1:09 PM

Dear Mr Foad,

I used two separate sub for each item 

a) Delete

b) Edit

but if you are not select any the sub a) will hang.

if you are not select any the sub b) will hang.

Kind regards,


Thursday, October 1, 2015 1:27 PM

Blow is your code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim zz As ListViewItem
        ListView1.BeginUpdate()
        zz = ListView1.Items.Add(Putext.Text)
        zz.SubItems.Add(Mxttext.Text)
        zz.SubItems.Add(Mxbtext.Text)
        zz.SubItems.Add(Myttext.Text)
        zz.SubItems.Add(Mybtext.Text)
        ListView1.Update()
        ListView1.EndUpdate()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim intselectedindex As Integer = ListView1.SelectedIndices(0)
        If intselectedindex >= 0 Then
            ListView1.Items(intselectedindex).SubItems(0).Text = Putext.Text
            ListView1.Items(intselectedindex).SubItems(1).Text = Mxttext.Text
            ListView1.Items(intselectedindex).SubItems(2).Text = Mxbtext.Text
            ListView1.Items(intselectedindex).SubItems(3).Text = Myttext.Text
            ListView1.Items(intselectedindex).SubItems(4).Text = Mybtext.Text
        Else
            MsgBox("There is no selection to modify", MessageBoxButtons.OK, MsgBoxStyle.Critical)
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim intselectedindex As Integer = ListView1.SelectedIndices(0)
        If intselectedindex >= 0 Then
            ListView1.Items(intselectedindex).Remove()
        Else
            MsgBox("There is no selection to delete", MessageBoxButtons.OK, MsgBoxStyle.Critical)
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.Hide()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim A(200) As ListViewItem
        Num = ListView1.Items.Count
        If Num = 0 Then
            MsgBox("There is no entered loads", MessageBoxButtons.OK, MsgBoxStyle.Critical)
            Me.Show()
        Else
            For q = 0 To Num - 1
                A(q) = ListView1.Items(q)
                PP(q) = A(q).SubItems(0).Text
                Muxt(q) = A(q).SubItems(1).Text
                Muxb(q) = A(q).SubItems(2).Text
                Muyt(q) = A(q).SubItems(3).Text
                Muyb(q) = A(q).SubItems(4).Text
            Next q

            Me.Hide()
        End If
    End Sub
End Class

I used two separate sub for each item 

a) Delete

b) Edit

but if you are not select any the sub a) will hang.

if you are not select any the sub b) will hang.

Kind regards,

Hany Metry


Thursday, October 1, 2015 3:37 PM

You've omitted a piece of the code that I sent and omitting that will cause the code to break if nothing is selected, I'm talking about this:

If listView1.SelectedIndices.Count <= 0 Then
        Return
    End If

Because in the below you are referring a selected index that does not exist becuase nothing is selected, which is the 0 index:

        Dim intselectedindex As Integer = ListView1.SelectedIndices(0)

Fouad Roumieh


Thursday, October 1, 2015 3:58 PM

Dim intselectedindex As Integer = ListView1.SelectedIndices(0)

the above Dim code make the sub hang.

Please check your code by try it before send your reply.


Thursday, October 1, 2015 4:47 PM

Please keep replies and comments civil here guys. Thanks!

Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com


Thursday, October 1, 2015 5:18 PM

Wouldn't this simplify it a bit?

        'modify
        If Me.listview1.SelectedItems.Count = 0 Then
            MsgBox("Please select an Item to modify", MessageBoxButtons.OK, MsgBoxStyle.Critical)
        Else
            With listview1.SelectedItems(0)
                .SubItems(0).Text = Putext.Text
                .SubItems(1).Text = Mxttext.Text
                .SubItems(2).Text = Mxbtext.Text
                .SubItems(3).Text = Myttext.Text
                .SubItems(4).Text = Mybtext.Text
            End With
        End If

        'remove
        If Me.listview1.SelectedItems.Count = 0 Then
            MsgBox("Please select an Item to Delete", MessageBoxButtons.OK, MsgBoxStyle.Critical)
        Else
            Me.listview1.Items.Remove(Me.listview1.SelectedItems(0))
        End If

Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com


Friday, October 2, 2015 5:45 AM

Below is the correct code:

Modify items:

If IsNothing(Me.ListView1.FocusedItem) Then

            MsgBox("Please select an Item to modify", MessageBoxButtons.OK, MsgBoxStyle.Critical)
        Else
            ListView1.Items(ListView1.FocusedItem.Index).SubItems(0).Text = Putext.Text
            ListView1.Items(ListView1.FocusedItem.Index).SubItems(1).Text = Mxttext.Text
            ListView1.Items(ListView1.FocusedItem.Index).SubItems(2).Text = Mxbtext.Text
            ListView1.Items(ListView1.FocusedItem.Index).SubItems(3).Text = Myttext.Text
            ListView1.Items(ListView1.FocusedItem.Index).SubItems(4).Text = Mybtext.Text
        End If

    End Sub

Delete items:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If IsNothing(Me.ListView1.FocusedItem) Then
            MsgBox("Please select an Item to Delete", MessageBoxButtons.OK, MsgBoxStyle.Critical)
        Else
            Me.ListView1.Items.RemoveAt(Me.ListView1.FocusedItem.Index)
        End If
    End Sub

Regards,

Hany metry

For those visiting this thread in the future seeking help on the same issue, the above solution cannot be a reliable one, simply because you can have cases where there is a FocusedItem but no SelectedItem and that happens when the ListView control has the focus(TabIndex=0 for example), whenever it has the focus the 1st item is set to FocusedItem by default. Also it is not logic to check for FocusedItem if it exists and then execute code for SelectedItem, its like someone is searching for "a" letter inside a string and then if found the code will be deleting "b" letter instead of "a".

There are many other examples given in this thread that can be used to answer the thread question whether by checking for SelectedIndices or SelectedItems.

Fouad Roumieh