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:
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
Comment