Adding text to a specific cell in listview...

lidds

Junior Contributor
Joined
Nov 9, 2004
Messages
210
I have a listview (lstView) that has four columns and two rows in it. I want to be able to click on a specific cell and add some text e.g. Hello to that cell only. Would this be acheived by adding a click method to a list subitem somehow.

Your help would be apreshiated, I am also writing this in vb.net

Cheers

Simon
 
If I understand you correctly, first you'll need to find which line in the listview was selected.

In you form's declaration section set a lineSelected variable
Visual Basic:
Dim lineSelected as Integer

Then in the listview's click event we'll get the selected line
Visual Basic:
    Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
        lineSelected = ListView1.SelectedItems(0).Index
    End Sub

from this point you can do anything to the selected line from anywhere in your form.

OR ... after re-reading your post again
Visual Basic:
    Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
        ListView1.Items(ListView1.SelectedItems(0).Index).SubItems(?).Text = "Hello"
    End Sub
 
This looks like what I need, except I need to determine which subitem I clicked so that I can replace your ? within your code with the column number as there will not always be the same amount of columns so I am unable to hard code this in.

Simon
 
Except for the first column (SubItems(0)), the only way to determine which column is clicked in a listview is by the mouse coordinates. Seems that a datagrid might serve you better.
 
Back
Top