[RESOLVED] [2008] Help with ListView
Hi, i'm trying to add some items into a specific ColumnHeader in ListBox1. If i have 3 columns, and i want to add the information written in Textbox1 to ColumnHeader2, how can i do it?
I add using:
Code:
ListView1.Items.Add("blah blah blah")
But that adds it into the first column... What can i do? Any ideas??:confused: :confused:
Re: [2008] Help with ListView
As I'm a stickler for terminology I'll point out that you're adding to ListView1, not ListBox1. Your code is right but your post is not. Also, you'll add the data to the column, not the column header. The column header is just the button-like area at the top, i.e. head, of the column that contains the column title.
As for the question, each item you add to the ListView represents a row. The Text of the item is the text displayed in the first column. If you want to display text in any other row then you must add subitems to the item. Note that, if you want an empty "cell", you must still add a subitem but leave its text blank. To populate three columns you might do this:
vb.net Code:
Me.ListView1.Items.Add(New ListViewItem(New String() {"col 1", "col 2", "col 3"}))
or this:
vb.net Code:
With Me.ListView1.Items.Add("col 1").SubItems
.Add("col 2")
.Add("col 3")
End With
Re: [2008] Help with ListView
Ohhh!! Great, thanks. But I have another question regarding that... If I have a text file with 3 words:
help me please
Can I "read" that file and then add those words to each column? Help in column 1, me in column 2 and please in column 3? By adding a range or do i do it by adding subitems?
Re: [2008] Help with ListView
vb Code:
Dim TextFileContent() As String = IO.File.ReadAllLines("path As String")
Will return a string() of the contents of a text file at a given path. Basic text parsing from there.
Re: [2008] Help with ListView
As ForumAccount says, ReadAllLines will return a String array where each element is a line from the file. You can then treat each element of the array as you can any other String. If you want to split a string into individual words then you can call its Split method and specify a space as the delimiter.
String.Split also returns a String array and you'll note that the first code snippet in my previous post shows you how to create a ListViewItem using a String array. That means that you can pass the result of String.Split directly to the ListViewItem constructor, e.g.
vb.net Code:
For Each line As String In IO.File.ReadAllLines("file path here")
Me.ListView1.Items.Add(New ListViewItem(line.Split(" "c)))
Next
Re: [2008] Help with ListView
Thanks, it works perfectly. But i just noticed, that i want it to skip one column...
Lets take the other example:
Help me please
I want Help to be in the first column, me in the second one and please in the fourth one, so that the third column can be free for another input. How can i do that? And also, I don't understand why there is a "c" in
Me.ListView1.Items.Add(New ListViewItem(line.Split(" "c))
Re: [2008] Help with ListView
Quote:
Originally Posted by tassa
Thanks, it works perfectly. But i just noticed, that i want it to skip one column...
Lets take the other example:
Help me please
I want Help to be in the first column, me in the second one and please in the fourth one, so that the third column can be free for another input. How can i do that?
This is from my first reply:
Quote:
Note that, if you want an empty "cell", you must still add a subitem but leave its text blank.
Quote:
Originally Posted by tassa
And also, I don't understand why there is a "c" in
Me.ListView1.Items.Add(New ListViewItem(line.Split(" "c))
The lower-case 'c' indicates a Char literal rather than a String literal that contains a single character.
Re: [2008] Help with ListView
Yeah, I got how to leave a "cell" empty, I forgot to edit the post.
Ok, well now I want to "fill" that empty cell with the text in a second combo box. But instead of entering the text in that third column in that row, it adds it in the next row, but in the correct column.
Re: [2008] Help with ListView
Like I said, each row in the ListView corresponds to a ListViewItem and each column corresponds to a ListViewSubItem of that item. If you want to set the text of column C in row R then you need to get the Cth subitem of the Rth item and set its Text property. Remember that, like all lists in VB.NET, the items and subitems of a ListView are zero-based, i.e. the first item is at index 0, which means that the Rth row is the item at index (R - 1) and the Cth subitem is the subitem at index (C - 1). So, to set the text in the Cth column of the Rth row to "Hello World" you would do this:
vb.net Code:
myListView.Items(R - 1).SubItems(C - 1).Text = "Hello World"
Note that to set the text in the first column you can either set the Text property of the item itself, or you can set the Text property of the first subitem.