Edit ListView SubItems

Thursday Jul 16th 2009 by Ronny Frister

Easily retrieve the subitem index and change the text from a ListView control.

This example explains an easy way to edit ListView subitems. A double click on the ListView.SubItem will visualize a TextBox overlaying the SubItem with same size. The entered signs will be written to the SubItem after TextBox_LostFocus event was raised (by clicking on the ListView control or hit RETURN key).

The following Visual Basic code is to be inserted into a form (e.g. MainForm). The Form contains a TextBox, Button and ListView control. TextBox and Button are hidden ([control].Visible = False)

For the listview the following settings apply:

  • set ListView.FullRowSelect = True
  • set ListView.GridLines = True
  • set ListView.Details = True

Remark: The LostFocus will not be raised if you click on the form. It will be called when you click on another control such as the ListView.

The Code

'declare a global variable to store the column index
Private selCol As Integer

'create ListView structure during FormLoad Event
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



        'load criteria column values into list view

        'create subitems
        'set the Tags for later identification of clicked subitem(index)
        For a = 0 To ListView42.Items.Count - 1
            For c = 1 To ListView42.Columns.Count - 1
                ListView42.Items(a).SubItems.Add(a & c).Tag = a & c
    End Sub

Private Sub ListView42_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView42.MouseDoubleClick

        'detect clicked subitem / column
        Dim hit As ListViewHitTestInfo = ListView42.HitTest(e.X, e.Y)

        For Each lItem As ListViewItem In ListView42.Items
            If lItem.Selected = True Then
                For a = 0 To lItem.SubItems.Count
                    If hit.SubItem.Tag = lItem.SubItems(a).Tag Then
                        selCol = a
                        'prevent edit on first column
                        If selCol = 0 Then Exit Sub
                        'dim size and location of the TextBox
                        'TextBox.FontSize maybe 1 bigger than that of
                        'Listview Items to get the users eye catched on it.
                        TextBox42.Left = ListView42.Left + hit.SubItem.Bounds.Left + 3
                        TextBox42.Top = ListView42.Top + hit.SubItem.Bounds.Top
                        TextBox42.Width = hit.SubItem.Bounds.Width
                        TextBox42.Text = hit.SubItem.Text
                        'set TextBox to visible for user input
                        TextBox42.Visible = True
                        Exit For
                    End If
            End If
    End Sub

    Private Sub TextBox42_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox42.KeyPress

        'call LostFocus Sub in the event user pressed RETURN
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            TextBox42_LostFocus(sender, Nothing)
        End If
    End Sub

    Private Sub TextBox42_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox42.LostFocus
        Dim selDetail As String

        'hide textbox
        TextBox42.Visible = False
        'load old value to detect if there is a change
        selDetail = ListView42.SelectedItems(0).SubItems(selCol).Text

        'change the subitem.text only if the new value is different to the existing one
        'to prevent display of the save butten without changing values
        If selDetail <> TextBox42.Text Then

  ListView42.Items(ListView42.SelectedItems(0).Index).SubItems(selCol).Text = TextBox42.Text

            'set change flag for not stored information to prevent loss of update by change of selection
            'changeFlage = True
            btnSave.Visible = True
        End If

        'do not clear the TextBox.Text, dueto LostFocus Sub may be called twice
        '(e.g. click mouse on a control different as the ListView)
        'else the list subitem.text will be overwritten again by an empty string

    End Sub

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved