Monday, August 13, 2007

Using a ListView in VB.NET

Here is a basic example of how to use the Detail View with a ListView in VB.NET.
See also AutoFitListView and CopyListViewToClipboard.

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

' Set ListView Properties
ListView1.View = View.Details
ListView1.GridLines = True
ListView1.FullRowSelect = True
ListView1.HideSelection = False
ListView1.MultiSelect = False

' Create Columns Headers
ListView1.Columns.Add("Column A")
ListView1.Columns.Add("Column B")
ListView1.Columns.Add("Column C")

Dim tempValue As Integer = 0

For i As Integer = 0 To 9

' Create List View Item (Row)
Dim lvi As New ListViewItem

' First Column can be the listview item's Text
lvi.Text = "Row " + i.ToString

' Second Column is the first sub item
tempValue = tempValue + 1

' Third Column is the second sub item
tempValue = tempValue + 1

' Add the ListViewItem to the ListView


End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles ListView1.SelectedIndexChanged

If ListView1.SelectedIndices.Count = 0 Then Return

Dim lvi As ListViewItem = _

MessageBox.Show(lvi.Text + " has been selected.")

End Sub


Anonymous said...

hmm..i can see i am the first one posting a comm abt this tutorial...
i must say that this tutorial is really interesting and has helped me a lot in my final year project.
hope to get more tutorial on on this site

Anonymous said...

very handy info. Thanks a lot

Anonymous said...

Thanks, helped me get my head around this... nice work

Anonymous said...

Thanks. After much searching, this is the first example that didn't lose me in discussing databases and icons.

teruterubozu13 said...

Thank you for the post. This helps me alot!

Anonymous said...

thanks ur info.....

Anonymous said...

Thank you. This was very helpful.

I love that you use your blog as a reference on your own digital shelf - lol What a great idea! How much time have I wasted searching through old projects looking for code when I could have it at my fingertips? And it gives back to the community too. Cool!

srego said...

Thanks. I have found the blog to very useful for finding my own stuff, and my posts show up on Google with everyone else's, so I don't even have to go to the blog to look things up.

nadeekadn said...

can i add check box for list view???

VB.Net Migration said...

Great information about