3 Replies - 5417 Views - Last Post: 11 April 2013 - 09:20 AM Rate Topic: -----

#1 JeffreyKiprotich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-April 13

Edit listview subitems

Posted 11 April 2013 - 01:02 AM

Hi;

Am trying to edit listview subitems in vb.net, but seemt the subitems in the listview control does not get any focus. What could i be doing wrong?
My code is as below:
Private Sub ListView3_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView3.MouseClick
        ' This subroutine checks where the double-clicking was performed and
        ' initiates in-line editing if user double-clicked on the right subitem

        ' check where clicked
        CurrentItem = ListView3.GetItemAt(e.X, e.Y)     ' which listviewitem was clicked
        If CurrentItem Is Nothing Then Exit Sub
        CurrentSB = CurrentItem.GetSubItemAt(e.X, e.Y)  ' which subitem was clicked

        ' See which column has been clicked

        ' NOTE: This portion is important. Here you can define your own
        '       rules as to which column can be edited and which cannot.
        Dim iSubIndex As Integer = CurrentItem.SubItems.IndexOf(CurrentSB)
        Select Case iSubIndex
            Case 1, 2, 3, 4
                ' These two columns are allowed to be edited. So continue the code
            Case Else
                ' In my example I have defined that only "Runs"
                ' and "Wickets" columns can be edited by user
                ListView3.LabelEdit = True
                ListView3.SelectedItems(0).BeginEdit()
                Exit Sub
        End Select

        Dim lLeft = CurrentSB.Bounds.Left + 2
        Dim lWidth As Integer = CurrentSB.Bounds.Width
        With TextBox3
            .SetBounds(lLeft + ListView3.Left, CurrentSB.Bounds.Top + _
                       ListView3.Top, lWidth, CurrentSB.Bounds.Height)
            .Text = CurrentSB.Text
            .Show()
            .Focus()
        End With
    End Sub
    Private Sub LV3_KeyDown(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView3.KeyDown
        ' This subroutine is for starting editing when keyboard shortcut is pressed (e.g. F2 key)
        If ListView3.SelectedItems.Count = 0 Then Exit Sub
        Select Case e.KeyCode
            Case Keys.F2    ' F2 key is pressed. Initiate editing
                e.Handled = True
                BeginEditListItem(ListView3.SelectedItems(0), 2)
        End Select
    End Sub
    Private Sub BeginEditListItem3(ByVal iTm As ListViewItem, ByVal SubItemIndex As Integer)
        ' This subroutine is for manually initiating editing instead of mouse double-clicks

        Dim pt As Point = iTm.SubItems(SubItemIndex).Bounds.Location
        Dim ee As New System.Windows.Forms.MouseEventArgs(Windows.Forms.MouseButtons.Left, 2, pt.X, pt.Y, 0)
        Call ListView3_MouseClick(ListView3, ee)
    End Sub
    Private Sub TextBox3_KeyPress(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
        ' This subroutine closes the text box

        Select Case e.KeyChar

            Case Microsoft.VisualBasic.ChrW(Keys.Return)    ' Enter key is pressed
                bCancelEdit = False ' editing completed
                e.Handled = True
                TextBox3.Hide()

            Case Microsoft.VisualBasic.ChrW(Keys.Escape)    ' Escape key is pressed
                bCancelEdit = True  ' editing was cancelled
                e.Handled = True
                TextBox3.Hide()

        End Select

    End Sub
    Private Sub TextBox3_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.LostFocus
        TextBox3.Hide()
        If bCancelEdit = False Then

            ' update listviewitem
            CurrentSB.Text = TextBox3.Text
            'CurrentSB.Text = CInt(TextBox1.Text).ToString("#,###0")

        Else

            ' Editing was cancelled by user
            bCancelEdit = False

        End If

        ListView3.Focus()

    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Edit listview subitems

#2 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 184
  • Joined: 26-April 10

Re: Edit listview subitems

Posted 11 April 2013 - 01:34 AM

Try the VB.Net forum.
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Edit listview subitems

Posted 11 April 2013 - 07:15 AM

There is not default method with the listview.
Here is a link where a textbox is used by placing it over the cell you want to edit

Or you could use a datagridview and remove the row headers and don't allow users to add new rows essentially making it look like a listview only with cell editing features. Plus you can set columns to read-only making them locked from edit.

Plus with the DGV, you get the added bonus of sorting by column header clicks, freezing left most panes at a certain column, etc...

This post has been edited by CharlieMay: 11 April 2013 - 07:16 AM

Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

Re: Edit listview subitems

Posted 11 April 2013 - 09:20 AM

Hi,
If you have your listview is set to (View Details) and want to get the Text, Row index, and Column index of the item in the listview that you double click on then here is a little program to do it. You would have to edit the text in a separate textbox and then save it back to the row and column. You must have the listviews (FullRowSelect) set to True for this to work. :bigsmile:/>
    Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
        Dim sp As New Point(MousePosition.X - Me.PointToScreen(ListView1.Location).X, MousePosition.Y - Me.PointToScreen(ListView1.Location).Y)
        Dim ht As ListViewHitTestInfo = ListView1.HitTest(sp)
        Select Case ht.Location
            Case ListViewHitTestLocations.Label
                Dim cwb, cwe, cnt As Integer
                For Each col As ColumnHeader In ListView1.Columns
                    cwe += col.Width
                    If sp.X > cwb And sp.X < cwe Then
                        Exit For
                    End If
                    cnt += 1
                    cwb = cwe
                Next
                MessageBox.Show("Selected Text=" & ht.SubItem.Text & "  Row Index=" & ht.Item.Index.ToString & "  Column Index=" & cnt.ToString)
        End Select
    End Sub


This post has been edited by IronRazer: 11 April 2013 - 09:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1