Editieren von ListView Items mittels TextBox

' auf dem Formular befindet sich ein ListView mit Namen ListView1
' alle Zellen des Formulars können editiert werden
Public Class Form1
    ' Textbox zum Editieren der Einträge
    Private _tbEdit As TextBox = New TextBox()
    Private _lvItemEdit As ListViewItem = Nothing
    Private _lvSubItemEdit As ListViewItem.ListViewSubItem = Nothing
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Textbox initialisieren
        _tbEdit.Visible = False
        ' Rahmen entfernen, damit die Textbox genau in die Zelle des ListViews passt
        _tbEdit.BorderStyle = BorderStyle.None
        ' Eventhandler für die Textbox hinzufügen
        AddHandler _tbEdit.Leave, AddressOf Me._tbEdit_Leave
        AddHandler _tbEdit.KeyPress, AddressOf Me._tbEdit_KeyPress
        ' der Form hinzufügen
        Me.Controls.Add(_tbEdit)
    End Sub
    
    Private Sub _tbEdit_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs)
        EndEditing(True)
    End Sub

    Private Sub _tbEdit_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

        Select Case e.KeyChar
            Case Chr(Keys.Escape)
                EndEditing(False)
            Case Chr(Keys.Enter)
                EndEditing(True)
        End Select

    End Sub
    
    Private Sub EndEditing(ByVal AcceptChanges As Boolean)
        If Not IsNothing(_lvSubItemEdit) Then
            If (AcceptChanges) Then
                _lvSubItemEdit.Text = _tbEdit.Text
            End If
        End If

        ' muss vor _tbEdit.Visible = False gesetzt werden,
        ' da das Event ein zweites mal durch _tbEdit_Leave aufgerufen wird
        _lvItemEdit = Nothing
        _lvSubItemEdit = Nothing

        _tbEdit.Visible = False
    End Sub

   Private Sub ListView1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
        Dim htInfo As ListViewHitTestInfo = ListView1.HitTest(e.X, e.Y)

        _lvItemEdit = Nothing
        _lvSubItemEdit = Nothing

        If Not IsNothing(htInfo) Then

            If Not IsNothing(htInfo.Item) And Not IsNothing(htInfo.SubItem) Then

                Dim iCol = htInfo.Item.SubItems.IndexOf(htInfo.SubItem)

                ' Alles außer Spalte 1 editieren
                If (iCol > 0) Then

                    _lvItemEdit = htInfo.Item
                    _lvSubItemEdit = htInfo.SubItem

                End If

            End If

        End If

    End Sub

    Private Sub ListView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
        If Not IsNothing(_lvItemEdit) And Not IsNothing(_lvSubItemEdit) Then
            ' Größe anpassen
            _tbEdit.Top = ListView1.Top + _lvSubItemEdit.Bounds.Top + 2
            _tbEdit.Left = ListView1.Left + _lvSubItemEdit.Bounds.Left + 2
            _tbEdit.Width = _lvSubItemEdit.Bounds.Width
            _tbEdit.Height = _lvSubItemEdit.Bounds.Height
            ' Inhalt übergeben
            _tbEdit.Text = _lvSubItemEdit.Text
            ' anzeigen
            _tbEdit.Visible = True
            _tbEdit.BringToFront()
            _tbEdit.Focus()
        End If
    End Sub
End Class