שתף באמצעות


Edit Listview subItem

Question

Tuesday, February 9, 2016 5:15 AM

Hi All.

This code found somewhere in internet.

when run and putting value in cell subitem ,it working but when we click the other subitem that cell in subitem return to old value.cannot change to new value.

thank.

Private Sub TextBox13_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim dtRow, dtCol As Integer

        ListView1.Items(dtRow).SubItems(dtCol).Text = TextBox13.Text
    End Sub


    Private Sub ListView1_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
        Dim iRow, iCol As Integer


        Dim hit As ListViewHitTestInfo = ListView1.HitTest(e.X, e.Y)
        Dim iWidth As Integer
        For iCol = 0 To hit.Item.SubItems.Count - 1
            If hit.Item.SubItems(iCol).Bounds.Left <= e.X Then
                If iCol = 0 And hit.Item.SubItems.Count > 1 Then
                    If e.X <= hit.Item.SubItems(1).Bounds.Left Then
                        iWidth = hit.Item.SubItems(1).Bounds.Left
                        Exit For
                    End If
                ElseIf e.X <= hit.Item.SubItems(iCol).Bounds.Right Then
                    iWidth = hit.Item.SubItems(iCol).Bounds.Width
                    Exit For
                End If
            End If
        Next
        iRow = hit.Item.Index
        TextBox13.Left = ListView1.Left + hit.SubItem.Bounds.Left + 3
        TextBox13.Top = ListView1.Top + hit.SubItem.Bounds.Top
        TextBox13.Width = iWidth
        TextBox13.Height = 18
        TextBox13.Text = hit.SubItem.Text
        TextBox13.Visible = True
        TextBox13.ReadOnly = False
    End Sub

All replies (6)

Tuesday, February 9, 2016 8:36 AM ✅Answered

Check this example it works on double click:

    Private hitinfo As ListViewHitTestInfo
    Private editbox As New TextBox()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        editbox.Parent = ListView1
        editbox.Hide()
        AddHandler ListView1.MouseDoubleClick, AddressOf listView_MouseDoubleClick
        CreateMyListView()
    End Sub
    Private Sub listView_MouseDoubleClick(sender As Object, e As MouseEventArgs)
        hitinfo = ListView1.HitTest(e.X, e.Y)
        editbox.Bounds = hitinfo.SubItem.Bounds
        editbox.Text = hitinfo.SubItem.Text
        AddHandler editbox.Leave, AddressOf editbox_LostFocus
        editbox.Select()
        editbox.Show()
    End Sub
    Private Sub editbox_LostFocus(sender As Object, e As EventArgs)
        hitinfo.SubItem.Text = editbox.Text
        editbox.Hide()
    End Sub

Populate ListView (msdn example):

 Private Sub CreateMyListView()

        ' Set the view to show details.
        ListView1.View = View.Details

        ' Allow the user to rearrange columns.
        ListView1.AllowColumnReorder = True

        ' Select the item and subitems when selection is made.
        ListView1.FullRowSelect = True
        ' Display grid lines.
        listView1.GridLines = True
        ' Sort the items in the list in ascending order.
        listView1.Sorting = SortOrder.Ascending

        ' Create three items and three sets of subitems for each item.
        Dim item1 As New ListViewItem("item1", 0)
        ' Place a check mark next to the item.
        item1.Checked = True
        item1.SubItems.Add("1")
        item1.SubItems.Add("2")
        item1.SubItems.Add("3")
        Dim item2 As New ListViewItem("item2", 1)
        item2.SubItems.Add("4")
        item2.SubItems.Add("5")
        item2.SubItems.Add("6")
        Dim item3 As New ListViewItem("item3", 0)
        ' Place a check mark next to the item.
        item3.Checked = True
        item3.SubItems.Add("7")
        item3.SubItems.Add("8")
        item3.SubItems.Add("9")

        ' Create columns for the items and subitems.
        ' Width of -2 indicates auto-size.
        listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
        listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
        listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
        listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)

        'Add the items to the ListView.
        listView1.Items.AddRange(New ListViewItem() {item1, item2, item3})
    End Sub 'CreateMyListView

Some of the code pieces referred from here with changes(1st comment)

Fouad Roumieh


Tuesday, February 9, 2016 6:24 AM

>>>This code found somewhere in internet.

But what you trying to do? I'm unable even to get this code working.

Fouad Roumieh


Tuesday, February 9, 2016 6:32 AM

Hi,

I just try writing to the subitem listview and maybe save that listview.

thank


Tuesday, February 9, 2016 6:39 AM

Yes but this whole click event handler is supposed to do?

If you want to add items and subitems to ListView, check the below example form msdn:

https://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.subitems%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

This is a runtime control you just need to call CreateMyListView and it will create and add the ListView to your Form controls.

Fouad Roumieh


Tuesday, February 9, 2016 7:12 AM

yup..

If want to put a Item or subitem very simple.Just declare  a ListViewItem and then we can put in there.

But too difficult to writing on subItem after on Leave that value still there.

thank


Tuesday, February 9, 2016 12:09 PM

thank,working good.