How Do I Add Items and SubItems to a Selected ListView Row?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gary108
    New Member
    • Dec 2012
    • 6

    How Do I Add Items and SubItems to a Selected ListView Row?

    Hello,

    In my application I have a form with a ListView control. I have three columns and I add text to each of those columns from three text boxes using a single Click Event.

    This works fine as long as I want to append the new items and subitems to the last line of my listView control. However, what I need sometimes is to edit the items and subitems at a specific row in the list.

    For example, I would like to select a certain row, add the items and subitems from the text boxes and have it add those items to that line, not the last line.

    My guess is that it could be done by identifying the index of a particular row and sending the items and subitems to that row based on the index. I expect that if there is already some item or subitem at that location, adding a new one will overwrite the existing one.

    I know how to select a row and determine the index but I cannot figure out how to add the items to a row of that particular index.

    Can anyone help me?


    This is my code:


    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItems.Click
    
            Dim strItem As String
            strItem = TextBox.Text
    
            Dim strItem1 As String
            strItem1 = TextBox1.Text
    
            Dim strItem2 As String
            strItem2 = TextBox2.Text
    
    
         Dim item1 As New ListViewItem(strItem)
         ListView1.Items.AddRange(New ListViewItem() {item1})
         item1.SubItems.Add(strItem1)
         item1.SubItems.Add(strItem2)
    
    
            TextBox.Clear()
            TextBox1.Clear()
            TextBox2.Clear()
    
        End Sub
    
    
    
      
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListView1.GridLines = True
            ListView1.View = View.Details
    
    
            ListView1.Columns.Add("Item Column", 150)
            ListView1.Columns.Add("Column 2", 100)
            ListView1.Columns.Add("Column 3", 100)
            ListView1.Font = New Font("times", 14)
            ListView1.FullRowSelect = True
    
        End Sub
        Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
            'Inserts selected ListView Item to the designated text box when the listview item is selected
            If ListView1.SelectedItems.Count > 0 Then
    
                Dim selectedItem As String
                selectedItem = ListView1.SelectedItems(0).Text
                TextBox.Text = selectedItem
            End If
    
            Dim subitem1 As String
            subitem1 = ListView1.SelectedItems(0).SubItems(1).Text()
            TextBox1.Text = subitem1
    
            Dim subitem2 As String
            subitem2 = ListView1.SelectedItems(0).SubItems(2).Text()
            TextBox2.Text = subitem2
    
            End Sub
    
    
    
    
    
        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim index As Integer = ListView1.SelectedIndices(0)
            txtIndex.text = index
    
        End Sub
    End Class
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    This is VB.NET so I'll go a head and move this to the VB.NET forum for you so it can be seen by the proper experts :)

    Comment

    • CyberSoftHari
      Recognized Expert Contributor
      • Sep 2007
      • 488

      #3
      [code=vb]
      Dim tmpListItem As ListItem ' Declaration should be out of add list function
      Set tmpListItem = UIlstItem.ListI tems.Add(intInd ex,strKey ,strDisplayText )
      tmpListItem.Sub Items(fieldInde x) = strDisplayStrin g2
      [/code]
      Above code format is an vb6 code and you can try this for VB.Net
      Last edited by CyberSoftHari; Dec 25 '12, 09:44 AM. Reason: quoted

      Comment

      • Gary108
        New Member
        • Dec 2012
        • 6

        #4
        More Information please?

        Originally posted by CyberSoftHari
        [code=vb]
        Dim tmpListItem As ListItem ' Declaration should be out of add list function
        Set tmpListItem = UIlstItem.ListI tems.Add(intInd ex,strKey ,strDisplayText )
        tmpListItem.Sub Items(fieldInde x) = strDisplayStrin g2
        [/code]
        Above code format is an vb6 code and you can try this for VB.Net
        Hello. Thank you for replying. I do not fully follow your code. Please explain the meaning of intIndex, strKey, and strDisplayText.

        Also, could you explain "fieldIndex and strDisplayStrin g2? Thank you.

        Comment

        • CyberSoftHari
          Recognized Expert Contributor
          • Sep 2007
          • 488

          #5
          UiListitems-> user interface listitems
          strDisplayText-> text to display in list items.
          intInex->row index(number)
          intFieldIndex->column index(number)
          strKEY->key value for row

          Comment

          Working...